单例double check

原创
2016/09/22 10:36
阅读数 91

 

double check模式 依然会导致潜在的资源的条件竞争

C++多线程模型 ,这种处理方法在大部分情况下能按照意图工作

通常做法
	Test*getInstance()
	{
		if (ins == nullptr)
		{
			m.lock();
			if (ins == nullptr)
			{
				ins = new Test;
			}
			m.unlock();
		}
		return ins;
	}

C11 还提供了另外一种做法,让runtime来保证

std::call_once std::once_flag 

展开阅读全文
加载中

作者的其它热门文章

0
0 收藏
分享
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部