BOOST scoped_lock in C ++ 11

I came across a situation where I need to replace BOOST scoped_lock with an equivalent in C ++ 11. Under visual studio 2013. Since C ++ 11 does not support scoped_lock, I'm not sure what the replacement code will be for the next one. Should I go for lock_guard or try_lock?

boost::mutex::scoped_lock objectLock(ObjectVectorMutex, boost::try_to_lock);
if(objectLock)
{
// code
}

And in the code I have the following wait statement

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

Any guidance is greatly appreciated.

+4
source share
1 answer

Use std::unique_lockinstead scoped_lock:

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

And it MotionThreadConditionwill be std::condition_variableused the same way. However, instead, if(condition)you must do the while(condition)right thing to handle false awakenings .

+6
source

Source: https://habr.com/ru/post/1533650/


All Articles