码迷,mamicode.com
首页 > 编程语言 > 详细

多线程单例模式的安全实现

时间:2014-12-03 01:46:07      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:c++11   thread   多线程   atomic_flag_init   stdbind   

多线程单例模式的实现。通过双重锁定,自旋处理,实现安全的多线程单例和共享区数据的访问。资源访问的形式采用简单等待处理.同时使用std::bind进行函数绑定。




// atomic_lock_flag_p202_6-3-3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <atomic>
#include <iostream>
#include <thread>
#include <Windows.h>
#include <vector>


using std::cout;
using std::cin;
using std::endl;

std::atomic_flag lock = ATOMIC_FLAG_INIT;
class TestLockInit
{
public:
	TestLockInit();
	~TestLockInit();

	static TestLockInit * TestLockInit::getInstance(void);

	void display(void)
	{
		//自旋尝试加锁
		while (lock.test_and_set(std::memory_order_acquire))
		{
			
		}
		cout<<"-----"<<++i<<endl;
		//释放互斥锁
		lock.clear(std::memory_order_release);
	}

private:
	static TestLockInit * s_pTestLockInit;
	static std::atomic_flag lock;
	std::atomic<int>i;
};

TestLockInit * TestLockInit::s_pTestLockInit = nullptr;
std::atomic_flag TestLockInit::lock = ATOMIC_FLAG_INIT;

TestLockInit * TestLockInit::getInstance(void)
{
	if (s_pTestLockInit==nullptr)
	{
		//自旋尝试加锁
		while (lock.test_and_set(std::memory_order_acquire))
		{
			cout<<"??????????????"<<endl;
		}
		//执行初始化
		if (s_pTestLockInit==nullptr)
		{
			s_pTestLockInit = new TestLockInit();
		}

		//释放互斥锁
		lock.clear(std::memory_order_release);
	}
	return s_pTestLockInit;
}

TestLockInit::TestLockInit():i(10)
{

}

TestLockInit::~TestLockInit()
{
}

void f(int n)
{
	while (lock.test_and_set(std::memory_order_acquire))
	{
		std::cout<<"Waiting from thread "<<n<<std::endl;
	}
	cout<< "Thred "<< n << " Starts working"<<endl;
	TestLockInit::getInstance()->display();

}


void g(int n)
{
	cout<< "Thread "<<n<< " is going to start."<<endl;
	lock.clear();
	cout<< "Thread "<<n<<" Starts working" <<endl;
	TestLockInit::getInstance()->display();

	
}
void h(void)
{
	auto a = std::bind(&TestLockInit::display,TestLockInit::getInstance());
	a();
}

int _tmain(int argc, _TCHAR* argv[])
{
	lock.test_and_set();//设为默认状态;

	/*std::thread t1(f,1);
	std::thread t2(g,2);

	t1.join();

	Sleep(100);
	t2.join();*/


	std::vector<std::thread> vthread;

	for (int i = 0; i < 10; ++i)
	{
		vthread.push_back(
			std::move(
				std::thread(h)
				)
			);
	}


	for(std::thread & th :vthread)
	{
		th.join();
	}

	return 0;
}


多线程单例模式的安全实现

标签:c++11   thread   多线程   atomic_flag_init   stdbind   

原文地址:http://blog.csdn.net/jingzhewangzi/article/details/41689185

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!