简体   繁体   中英

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
{

}

}//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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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