0

Initially I was using boost::mutex::scoped_lock as such (which worked)

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

However later I changed the lock to the following which does not work

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

Any suggestions on how to resolve the issue I get intellisense error stating No instance of the overloaded function matches the argument list. The compile error is

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> &'  
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • just a point: you never KNOW that a concurrent algorithm works. unless you mathematically prove it. which cannot be done because the compiler and the hardware are not provable. – v.oddou Mar 27 '14 at 05:22

1 Answers1

3

boost::lock_guard doesn't have unlock member-function, which is needed for condition. Use unique_lock instead:

boost::unique_lock<boost::mutex> lock(mut);
condition.wait(lock);
Igor R.
  • 14,716
  • 2
  • 49
  • 83
  • From what I have read is that unique_lock is more advanced than lock_guard. Will I have to make any further changes if I use unique_lock ? – Rajeshwar Apr 16 '13 at 17:59
  • @Rajeshwar Correct, `lock_guard` is a very trivial RAII locking facility, and its capabilities are insufficient for `condition`. You can use `lock_guard` with no additional changes - it's also RAII lock, but with additional functionality. – Igor R. Apr 16 '13 at 18:01