As per title, how to try_lock on a boost::unique_lock ?
I've this code:
void mySafeFunct()
{
if(myMutex.try_lock() == false)
{
return -1;
}
// mutex ownership is automatically acquired
// do stuff safely
myMutex.unlock();
}
Now I'd like to use a unique_lock (which is also a scoped mutex) instead of the plain boost::mutex. I want this to avoid all the unlock() calls from the function body.
You can either defer the locking with the Defer constructor, or use the the Try constructor when creating your unique_lock
:
boost::mutex myMutex;
boost::unique_lock<boost::mutex> lock(myMutex, boost::try_lock);
if (!lock.owns_lock())
return -1;
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With