2014-06-18 78 views
1

我有一个看起来像代码:提升作用域解锁互斥量

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

while (true) 
{ 
    ... 

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

    ... 

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

我希望做这样的事情:

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

while (true) 
{ 
    ... 

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

     ... 
    } 

} 

我几乎可以肯定在此之前,我已经看到了这一点......或至少是讨论它,但我找不到它。

+0

你可能用两个作用域锁来重写代码,而忽略'scoped_unlock' –

回答

4

您正在寻找如Boost.Threads Reverse Lock

reverse_lock扭转锁的操作:它提供RAII风格,即在解锁施工时间锁定和破坏的时间锁定。另外,它暂时转移所有权,以便无法使用锁定来锁定互斥锁。