我可以在超出scoped_lock作用域之前解锁互斥锁吗?我怎么能这么做呢?
{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
{
}
}//lock scope谢谢。
发布于 2011-01-11 17:06:23
是。
使用unlock()方法。
{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发布于 2011-01-11 17:06:10
可以;只需使用.unlock()成员函数。
发布于 2011-01-11 17:30:57
boost::mutex::scoped_lock与boost::unique_lock<mutex>相同,您可以解锁它们。它必须被你的线程锁定才能这样做,否则你会得到一个异常。
unique_lock的析构函数确保互斥锁在销毁时被解锁,因此使用lock对象的目的是确保在持有锁的任何时候抛出异常时的这一点(异常安全性)。
https://stackoverflow.com/questions/4655850
复制相似问题