C++单例模式:单例模式遇到多线程

单例模式介绍

单例模式主要有2中形式,一种是饿汉式,一种是懒汉式

饿汉式:程序一加载单例模式就已经创建了,也就很饥饿嘛。因为是静态属性进行单例初始化,所以优点是线程是安全的缺点无论用户是否使用单例对象都会创建单例对象

懒汉式:当用户使用单例对象时,才去创建单例对象,所以很懒惰嘛。优点是用户不使用就不会创建对象,缺点是 当遇到多线程是,是线程不安全的,但是我们可以使用加强版的也就是线程安全的懒汉式下面我们会进行讲解

饿汉式和懒汉式代码

代码展示

#include 

using namespace std;
class Singleton_Hungry
{
private:
	Singleton_Hungry()
	{
		cout << "我是饿汉式,在程序加载时,我就已经存在了。" << endl;
	}
	static Singleton_Hungry* singleton;
public:
	static Singleton_Hungry* getInstace()
	{
		return singleton;
	}

};
//静态属性类外初始化
Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry;

class Singleton_Lazy
{
private:
	Singleton_Lazy()
	{
		cout << "我是懒汉式,在别人需要我的时候,我才现身。" << endl;
	}
	static Singleton_Lazy* singleton;
public:
	static Singleton_Lazy* getInstance()
	{
		
		if (NULL == singleton)
		{
			singleton = new Singleton_Lazy;
		}
		return singleton;
	}
};
Singleton_Lazy* Singleton_Lazy::singleton = NULL;

int main(int argc, char *argv[])
{
	Singleton_Hungry *hungry1 = Singleton_Hungry::getInstace();
	Singleton_Hungry *hungry2 = Singleton_Hungry::getInstace();
	cout << "hungry1地址:" << hungry1 << ",hungry2地址:" << hungry2 << endl;
	Singleton_Lazy *lazy1 = Singleton_Lazy::getInstance();
	Singleton_Lazy *lazy2 = Singleton_Lazy::getInstance();
	cout << "lazy1地址:" << lazy1 << ",lazy2地址:" << lazy1 << endl;

	return 0;
}

运行结果

C++单例模式:单例模式遇到多线程_第1张图片

懒汉式遇到多线程

由于懒汉式在用户需要是才进行单例对象的创建,如果遇到多线程容易发生内存泄漏,我们可以用c++中的线程互斥对象mutex来进行加强多线程用thread类进模拟

代码展示

#include 
#include 
#include 

using namespace std;
mutex mu;//线程互斥对象
class Singleton_Hungry
{
private:
	Singleton_Hungry()
	{
		cout << "我是饿汉式,在程序加载时,我就已经存在了。" << endl;
	}
	static Singleton_Hungry* singleton;
public:
	static Singleton_Hungry* getInstace()
	{
		return singleton;
	}

};
//静态属性类外初始化
Singleton_Hungry* Singleton_Hungry::singleton = new Singleton_Hungry;

class Singleton_Lazy
{
private:
	Singleton_Lazy()
	{
		cout << "我是懒汉式,在别人需要我的时候,我才现身。" << endl;
	}
	static Singleton_Lazy* singleton;
public:
	static Singleton_Lazy* getInstance()
	{

		if (NULL == singleton)
		{
			
			mu.lock();//关闭锁
			if (NULL == singleton)
			{
				singleton = new Singleton_Lazy;
			}
			mu.unlock();//打开锁
		}
		return singleton;
	}
};
Singleton_Lazy* Singleton_Lazy::singleton = NULL;
void thread01()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "thread01 working...." << endl;
		Singleton_Lazy *lazy1 = Singleton_Lazy::getInstance();
		cout << "thread01创建单例lazy1地址:" << lazy1 << endl;
	}
}
void thread02()
{
	for (int i = 0; i < 5; i++)
	{
		cout << "thread02 working...." << endl;
		Singleton_Lazy *lazy2 = Singleton_Lazy::getInstance();
		cout << "thread02创建单例lazy2地址:" << lazy2 << endl;
	}
}

int main(int argc, char *argv[])
{
	thread thread1(thread01);
	thread thread2(thread01);
	thread1.detach();
	thread2.detach();
	for (int i = 0; i < 5; i++)
	{
		cout << "Main thread working..." << endl;
		Singleton_Lazy *main = Singleton_Lazy::getInstance();
		cout << "Main 创建单例lazy地址:" << main << endl;
	}
	return 0;
}

多线程下线程不安全的懒汉式测试

下图是将40行45行线程互斥对象操作注释的情况,发生了内存泄漏,创建了多个单例。图中字打错了。。。

C++单例模式:单例模式遇到多线程_第2张图片

多线程下线程安全的懒汉式测试

C++单例模式:单例模式遇到多线程_第3张图片

你可能感兴趣的:(【Language_C++】,【设计模式】,设计模式(C++版))