简体   繁体   中英

boost scoped unlock for a mutex

I have a code that looks like:

boost::mutex::scoped_lock lck(mQueueMutex);

while (true)
{
    ...

    // unlock the queue while we exec the job
    lck.unlock();

    ...

    // lock the queue again
    lck.lock();
}

I am looking to do something like this:

boost::mutex::scoped_lock lock(mQueueMutex);

while (true)
{
    ...

    // unlock the queue while we exec the job
    {
        boost::mutex::scoped_unlock unlock(lock);

        ...
    }

}

I am almost sure that I have seen this before ... or at least discussion about it, but I can't find it.

You are looking for Boost.Threads Reverse Lock :

reverse_lock reverse the operations of a lock: it provide for RAII-style, that unlocks the lock at construction time and lock it at destruction time. In addition, it transfer ownership temporarily, so that the mutex can not be locked using the Lock.

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