C++并行开发7-单例设计模式共享数据分析、解决,call_once

FengLY Lv3

单例模式

#include <iostream>
#include <thread>

using namespace std;

class MyCAS {
private:
    MyCAS() {};
    static MyCAS *m_instance;

public:
    static MyCAS *Getinstance() {
        if (m_instance == NULL) {
            m_instance = new MyCAS();
            static huishou cl;
        }
        return m_instance;
    }
    class huishou {
    public:
        ~huishou() {
            if (MyCAS::m_instance) {
                cout << "单例类的析构" << endl;
                delete MyCAS::m_instance;
                MyCAS::m_instance = NULL;
            }
        }
    };

    void func() {
        cout << this << endl;
    }
};

MyCAS * MyCAS::m_instance = NULL;

int main() {
    MyCAS* p_a = MyCAS::Getinstance();
    MyCAS* p_b = MyCAS::Getinstance();

    p_a->func();
    p_b->func();

    return 0;
}

单例模式共享数据分析

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;
std::mutex resource_mutex;

class MyCAS {
private:
    MyCAS() {};
    static MyCAS *m_instance;

public:
    static MyCAS *Getinstance() {
        if (m_instance == NULL) {  //双重锁定, 提高效率
            std::unique_lock<std::mutex> lock_mutex(resource_mutex);
            if (m_instance == NULL) {
                m_instance = new MyCAS();
                static huishou cl;
            }
            return m_instance;
        }
    }
    class huishou {
    public:
        ~huishou() {
            if (MyCAS::m_instance) {
                cout << "单例类的析构" << endl;
                delete MyCAS::m_instance;
                MyCAS::m_instance = NULL;
            }
        }
    };

    void func() {
        cout << this << endl;
    }
};

MyCAS * MyCAS::m_instance = NULL;

void threadFunc() {
    cout << "线程开始" << endl;
    MyCAS *p_a = MyCAS::Getinstance();
    cout << "线程执行完毕" << endl;
}

int main() {
    std::thread mytobj1(threadFunc);
    std::thread mytobj2(threadFunc);  //有可能出现两个线程同时new, 因此需要加互斥量

    mytobj1.join();
    mytobj2.join();
}

加互斥量的同时,使用双重锁定策略可以有效的提高效率

std::call_once()

#include <iostream>
#include <thread>
#include <mutex>
using namespace std;

std::mutex resource_mutex;
std::once_flag g_flag;  //现在这个是个系统定义的标记
class MyCAS {
private:
    MyCAS() {};
    static MyCAS *m_instance;

public:
    static void generateInstance() {
        //测试
        cout << "正在创建线程" << endl; 
        std::chrono::milliseconds dura(20000);
        std::this_thread::sleep_for(dura);

        m_instance = new MyCAS();
        static huishou cl;
    }
    static MyCAS *Getinstance() {
        if (m_instance == NULL) {  //双重锁定, 提高效率
            /*std::unique_lock<std::mutex> lock_mutex(resource_mutex);
            if (m_instance == NULL) {
                m_instance = new MyCAS();
                static huishou cl;
            }*/
            std::call_once(g_flag, generateInstance);  //两个线程执行到这里相当于有一个 互斥量
            return m_instance;
        }
    }
    class huishou {
    public:
        ~huishou() {
            if (MyCAS::m_instance) {
                cout << "单例类的析构" << endl;
                delete MyCAS::m_instance;
                MyCAS::m_instance = NULL;
            }
        }
    };

    void func() {
        cout << this << endl;
    }
};

MyCAS * MyCAS::m_instance = NULL;

void threadFunc() {
    cout << "线程开始" << endl;
    MyCAS *p_a = MyCAS::Getinstance();
    cout << "线程执行完毕" << endl;
}

int main() {
    //call_once C++11引入的新函数,该函数的第二个参数是函数名 
    //call_once的功能是能保证a()只被调用一次
    //具备互斥量的能力,在效率上比互斥量效率高
    //call_once需要和一个标记结合使用,这个标记是std::once_flag,调用成功后这个标记被设置成一种已被调用的状态
    std::thread mytobj1(threadFunc);
    std::thread mytobj2(threadFunc);  //有可能出现两个线程同时new, 因此需要加互斥量

    mytobj1.join();
    mytobj2.join();

    return 0;
}
  • call_once C++11引入的新函数,该函数的第二个参数是函数名
  • call_once的功能是能保证a()只被调用一次
  • 具备互斥量的能力,在效率上比互斥量效率高
  • call_once需要和一个标记结合使用,这个标记是std::once_flag,调用成功后这个标记被设置成一种已被调用的状态
  • Title: C++并行开发7-单例设计模式共享数据分析、解决,call_once
  • Author: FengLY
  • Created at : 2023-06-18 22:40:09
  • Updated at : 2023-06-18 22:57:30
  • Link: https://zhouaq.com/2023/06/18/C++并行开发7-单例设计模式共享数据分析、解决,call_once/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments
On this page
C++并行开发7-单例设计模式共享数据分析、解决,call_once