boost::scoped_lock unlock
Could I unlock the mutex before out of the scope of scoped_lock? How could I do that?
{boost::mutex::scoped_lock lock(mutex);
if(conditionaA)
{
if(conditionB)
{
//could I unlock here as I don't want to hold the lock too long.
//perform calculation
}
}
else
{
开发者_如何学Python}
}//lock scope
Thanks.
Yes.
Use the unlock()
method.
{boost::mutex::scoped_lock lock(mutex);
if(conditionaA)
{
if(conditionB)
{
//could I unlock here as I don't want to hold the lock too long.
lock.unlock(); // <--
}
//perform calculation
}
else
{
}
}//lock scope
Yes; just use the .unlock() member function.
boost::mutex::scoped_lock
is the same as boost::unique_lock<mutex>
and you can unlock them. It must be locked by your thread to do this or you get an exception.
The destructor of unique_lock ensures that the mutex is unlocked at destruction time, and the purpose of using the lock object is thus to ensure this (exception safety) if an exception is thrown at any time the lock is held.
精彩评论