2014-06-18 84 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風格,即在解鎖施工時間鎖定和破壞的時間鎖定。另外,它暫時轉移所有權,以便無法使用鎖定來鎖定互斥鎖。