单例模式懒汉模式和饿汉模式

线程安全

单例模式在单线程中,当然是安全的。但是如果在多线程中,由于并行判断,可能会导致创建多个实例。那么如何保证在多线程中单例还是只有一个实例呢?

常见的三种方式:

局部静态变量

原理和饿汉模式相似,利用static只会初始化一次的特性,并且在第一次调用的情况下才会被初始化。推荐使用

class Singleton {

private:

Singleton() { };

public:

static Singleton* getInstance() {

static Singleton *instance = new Singleton();

return instance;

}

};

饿汉模式

原理:利用static,在程序编译的时候就调用构造函数实现单例,这样做的优点是保证线程安全,但是缺点就是无论后续是否用到,在编译的时候就会创建,会导致启动性能降低。

实现方法:

class Singleton_Hungry {

public:

static Singleton_Hungry* getInstance() {

return singleton;

}

private:

Singleton_Hungry() {

cout << "Hungry creat." << endl;

}

static Singleton_Hungry* singleton;

};

Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry();

懒汉模式

原理:利用线程锁,在获取实例的时候判断实例加上线程锁,保证判断的条件只允许一个线程操作。利用锁也可以保证单例只有一个实例。

实现方法:

#include <mutex>

std::mutex mu;

class Singleton_Lazy {

public:

static Singleton_Lazy* getInstance() {

if (singleton == NULL) {

mu.lock();//打开锁

if (singleton == NULL) {

singleton = new Singleton_Lazy();

}

mu.unlock();//关闭锁

}

return singleton;

}

private:

Singleton_Lazy() {

cout << "Lazy creat." << endl;

}

static Singleton_Lazy* singleton;

};

Singleton_Lazy* Singleton_Lazy::singleton = NULL;

实践验证

在linux系统上通过命令行g++ single.cpp --std=c++11 -lpthread编译

#include <iostream>

#include <mutex>

#include <thread>

#include <unistd.h>

using namespace std;

mutex mu;

class Singleton_Hungry {

public:

static Singleton_Hungry* getInstance() {

return singleton;

}

private:

Singleton_Hungry() {

cout << "Hungry creat." << endl;

}

static Singleton_Hungry* singleton;

};

Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry();

class Singleton_Lazy {

private:

Singleton_Lazy() {

cout << "Lazy creat." << endl;

}

static Singleton_Lazy* singleton;

public:

static Singleton_Lazy* getInstance() {

if (singleton == NULL) {

//mu.lock();//打开锁

if (singleton == NULL) {

singleton = new Singleton_Lazy();

}

//mu.unlock();//关闭锁

}

return singleton;

}

};

Singleton_Lazy* Singleton_Lazy::singleton = NULL;

void thr(int t) {

cout << t << " pthread id: " << pthread_self() << endl;

for(int i = 0; i < 3; i++) {

Singleton_Lazy *lazy = Singleton_Lazy::getInstance();

Singleton_Hungry *hungry = Singleton_Hungry::getInstance();

cout << t << " lazy addr:" << lazy << endl;

cout << t << " hungry addr:" << hungry << endl;

}

}

int main() {

cout<<"main process id: "<<getpid()<<endl;

cout<<"main pthread id:"<< pthread_self()<<endl;

thread thread1(thr, 1);

thread thread2(thr, 2);

thread1.join();

thread2.join();

return 0;

}

结果分析

结果和预想一致,饿汉模式在程序编译阶段调用构造函数,懒汉模式在调用的时候创建,如果不加线程锁会导致创建多个实例。

【C++】保证线程安全的单例模式_c++ 线程安全单例模式-CSDN博客

相关推荐
西北大程序猿7 小时前
单例模式与锁(死锁)
linux·开发语言·c++·单例模式
找不到、了2 天前
实现单例模式的常见方式
java·开发语言·单例模式
不愧是你呀5 天前
C++中单例模式详解
网络·c++·windows·单例模式
变身缎带5 天前
Unity中的MonoSingleton<T>与Singleton<T>
unity·单例模式·c#·游戏引擎
小吴同学·7 天前
OPC Client第6讲(wxwidgets):Logger.h日志记录文件(单例模式);登录后的主界面
开发语言·c++·单例模式·wxwidgets
勤奋的知更鸟8 天前
Java 单例模式详解
java·开发语言·单例模式
ailinghao8 天前
单例模式的类和静态方法的类的区别和使用场景
flutter·单例模式
XiaoLeisj8 天前
【JUC】深入解析 JUC 并发编程:单例模式、懒汉模式、饿汉模式、及懒汉模式线程安全问题解析和使用 volatile 解决内存可见性问题与指令重排序问题
javascript·安全·单例模式
charlie1145141919 天前
从C++编程入手设计模式1——单例模式
c++·单例模式·设计模式·架构·线程安全
linux-hzh9 天前
设计模式之单例模式
单例模式·设计模式