在C++ 11中使用BOOST scoped_lock替换

Waj*_*jih 5 c++ boost c++11

我面临的情况是我必须用C++ 11中的等效替换BOOST scoped_lock.在visual studio 2013下.由于c ++ 11不支持scoped_lock,我不确定以下内容的替换代码是什么.我应该去lock_guard还是try_lock?

boost::mutex::scoped_lock objectLock(ObjectVectorMutex, boost::try_to_lock);
if(objectLock)
{
// code
}
Run Code Online (Sandbox Code Playgroud)

在代码中我有以下'wait'语句

if(ObjectsCollection.empty())
{
//This is where we wait till something is filled
MotionThreadCondition.wait(objectLock);
ElapsedTime = 0;            
}
Run Code Online (Sandbox Code Playgroud)

任何指导都非常感谢.

Dav*_*vid 7

使用std::unique_lock而不是scoped_lock:

std::unique_lock objectLock(ObjectVectorMutex, std::try_to_lock);
Run Code Online (Sandbox Code Playgroud)

并且MotionThreadCondition会以std::condition_variable同样的方式使用.但是,if(condition)您应该正确while(condition)处理虚假唤醒.