您的位置:首页 > 其它

多线程安全的单例模式(使用判断nullptr和call_once两种方法)

2021-04-10 21:31 65 查看

转载请注明: 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;
}
ad8 [/code]

注意:
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或者全局对象,否则不同线程间不可见,则无法起到作用.

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

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐