繁体   English   中英

C++11 中的 BOOST scoped_lock 替换

[英]BOOST scoped_lock replacement in 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) {
  // ...
}

在代码中,我有以下“等待”语句

if (ObjectsCollection.empty()) {
  // This is where we wait til something is filled
  MotionThreadCondition.wait(objectLock);
  ElapsedTime = 0;          
}

非常感谢任何指导。

使用std::unique_lock而不是scoped_lock

std::unique_lock objectLock(ObjectVectorMutex, std::try_to_lock);

MotionThreadCondition将是std::condition_variable ,使用方式相同。 但是,您应该使用while(condition)来正确处理虚假唤醒,而不是if(condition)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM