使用boost :: mutex :: scoped_lock来提升:: condition_variable

时间:2013-04-16 17:36:35

标签: boost locking boost-thread

最初我使用的是boost::mutex::scoped_lock(有效)

boost::mutex::scoped_lock lock(mutex_name); 
condition.wait(lock); //where condition = boost::condition_variable

但是后来我将锁更改为以下无法正常工作

boost::lock_guard<boost::mutex> lock(mutex_name)
condition.wait(lock); //Error

有关如何解决问题的任何建议我得到intellisense错误说明No instance of the overloaded function matches the argument list。编译错误是

Error   7   error C2664: 'void boost::condition_variable::wait(boost::unique_lock<Mutex> &)' : cannot convert parameter 1 from 'boost::lock_guard<Mutex>' to 'boost::unique_lock<Mutex> &'  

1 个答案:

答案 0 :(得分:3)

boost::lock_guard没有unlock成员函数,这是condition所需的。请改用unique_lock

boost::unique_lock<boost::mutex> lock(mut);
condition.wait(lock);