轉載請註明: https://blog.csdn.net/Stephen___Qin/article/details/115583694
使用判斷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或者全域性物件,否則不同執行緒間不可見,則無法起到作用.