c++多线程(六) - 单例模式

    单例模式是设计模式的一种。程序中有一些特殊的类,在程序运行期间只能有一个对象。

1、单线程

     该类的构造函数为私有,且包含一个静态的成员变量m_instance和成员函数GetInstance()。使用时通过GetInstance()返回该类的指针。

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

class MyCAS
{
public:
	static MyCAS *GetInstance()
	{
		if (m_instance == NULL)
		{
			m_instance = new MyCAS();
		}
		return m_instance;
	}	
private:
	MyCAS() {}
	static MyCAS *m_instance;
};

MyCAS *MyCAS::m_instance = NULL;
int main()
{
	MyCAS *ptr = MyCAS::GetInstance();
	return 0;
}

  2、多线程

    上述代码在单线程的情况下没有问题,但是在多线程的情况下如何呢?肯定会出问题的!

    考虑如下情况,m_instance为空,两个线程a, b 同时执行函数GetInstance()。则m_instance = new MyCAS()可能会执行多次,程序中对象就会有多个,这显然违背了设计的初衷。

    为了使代码在多线程的情况下正确运行,我们重构上述代码。注意双重锁定是为了提高程序运行效率(在m_instance不为空时,不再加锁,立即返回m_instance)。

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

mutex resource_mutex;
class MyCAS
{
public:
	static MyCAS *GetInstance()
	{
		//双重锁定
		if (m_instance == NULL)
		{
			unique_lock<mutex> guard(resource_mutex);
			if (m_instance == NULL)
			{
				m_instance = new MyCAS();
			}
		}	
		return m_instance;
	}	
private:
	MyCAS() {}
	static MyCAS *m_instance;
};

MyCAS *MyCAS::m_instance = NULL;
void MyFunc()
{
	cout << "线程开始" << endl;
	MyCAS *ptr = MyCAS::GetInstance();
	cout << "线程结束" << endl;
}
int main()
{
	thread obj1(MyFunc);
	thread obj2(MyFunc);
	obj1.join();
	obj2.join();
	return 0;
}

3.call_once()

    call_once()保证函数只被调用一次。它通过标记once_flag记录函数是否执行过。如果一个线程调用call_once()成功后,once_flag就会被标记为“已调用”。其他线程再次调用call_once()时,就不会再执行该函数了。

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

once_flag g_flag;
class MyCAS
{
public:
	static void CreatInstance()
	{		
		m_instance = new MyCAS();
	}
	static MyCAS *GetInstance()
	{
		call_once(g_flag, CreatInstance);
		return m_instance;
	}	
private:
	MyCAS() {}
	static MyCAS *m_instance;
};

MyCAS *MyCAS::m_instance = NULL;
void MyFunc()
{
	cout << "线程开始" << endl;
	MyCAS *ptr = MyCAS::GetInstance();
	cout << "线程结束" << endl;
}
int main()
{
	thread obj1(MyFunc);
	thread obj2(MyFunc);
	obj1.join();
	obj2.join();
	return 0;
}

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值