多線程安全的單例模式(使用判斷nullptr和call_once兩種方法)

轉載請註明: https://blog.csdn.net/Stephen___Qin/article/details/115583694ios

使用判斷nullptr

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

class Singleton
{
private:
    Singleton()
    {
    }
    
    static Singleton * m_singleton;//C++類中不能夠定義本身類的對象,可是能夠定義本身類的指針和引用.
    
public:
    static Singleton * getInstance();
};

Singleton * Singleton::m_singleton = nullptr;
Singleton * Singleton::getInstance()
{
    if(m_singleton == nullptr)
        m_singleton = new Singleton();
    
    return m_singleton;
}

void ThreadFunc()
{
    Singleton *s = Singleton::getInstance();
    std::cout << "s:" << s << std::endl;
}

int main()
{
    thread t1(ThreadFunc);
    t1.join();

    thread t2(ThreadFunc);
    t2.join();

    thread t3(ThreadFunc);
    t3.join();

    return 0;
}

注意:
1.構造函數要定義爲private,這樣就沒法建立對象,保證只能經過類名來訪問單例.
2.static變量須要在類外初始化.爲何呢?由於靜態變量不屬於某個對象,而是屬於類,若是放在類內初始化,則變成了這個對象的了,這就和以前的假設矛盾了函數

使用call_once

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

static std::once_flag of;

class Singleton
{
private:
    Singleton()
    {
    }
    
    static Singleton * m_singleton;
    
public:
    static Singleton * getInstance();
};

Singleton * Singleton::m_singleton = nullptr;

Singleton * Singleton::getInstance()
{
    std::call_once(of, []()
    {
        m_singleton = new Singleton();    
    }
    );

    return m_singleton;
}

void ThreadFunc()
{
    Singleton *s = Singleton::getInstance();
    std::cout << "s:" << s << std::endl;
}

int main()
{
    thread t1(ThreadFunc);
    t1.join();

    thread t2(ThreadFunc);
    t2.join();

    thread t3(ThreadFunc);
    t3.join();

    return 0;
}

注意:
1.call_once和once_flag的頭文件是<mutex>
2.once_flag定義爲static或者全局對象,不然不一樣線程間不可見,則沒法起到做用.spa

參考文章:
https://zhuanlan.zhihu.com/p/71900518.net

相關文章
相關標籤/搜索