首页 > 其他 > 详细

单实例模式的实现

时间:2014-04-21 03:26:58      阅读:409      评论:0      收藏:0      [点我收藏+]
template <class T>
class ISingleton
{
public:
	static T& Instance()
	{
		static void* volatile pInstance = NULL;
		static void* volatile creating = NULL;
		if(pInstance)
			return pInstance;
		if(InterlockedCompareExchangePointer(&creating, (void*)1, 0) == 0)
		{
			static T instance;
			InterlockedExchangePointer(&pInstance, &instance);
		}else
		{
			if(!pInstance)
				Sleep(1);
		}
		return pInstance;
	}
private:
	ISingleton();
	ISingleton(const ISingleton&);
	ISingleton& operator=(const ISingleton&);
};

单实例模式的实现,布布扣,bubuko.com

单实例模式的实现

原文:http://blog.csdn.net/x313695373/article/details/24192053

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!