繁体   English   中英

boost :: scoped_lock解锁

[英]boost::scoped_lock unlock

我可以在超出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

谢谢。

是。

使用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

是; 只需使用.unlock()成员函数。

boost::mutex::scoped_lockboost::unique_lock<mutex> ,您可以解锁它们。 为此,必须由您的线程将其锁定,否则您将获得异常。

unique_lock的析构函数可确保互斥对象在销毁时被解锁,因此,使用锁定对象的目的是确保在保持锁定的任何时间抛出异常时,都可以确保此操作(异常安全)。

暂无
暂无

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

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