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