Subject::[boost thread] does a thread block itself when it passes the conditional variable to another thread?

inside a thread, if the conditional variable is changed to satisfy another thread which is on the waiting thread list. Does the current thread give up the global mutex and blocks itself until another thread gives up the mutex? boost::mutex mutex; boost::condition cond; bool data_ready=false; : void thread1(){ boost::mutex::scoped_lock lock(mutex); ...//do some work data_ready=true; cond.notify_one() //assume the condition is satisfied. //do more work -------my question is will this part continue? or thread2 will wake up and block thread 1? } void thread2(){ boost::mutex::scoped_lock lock(mutex); while ( !data_ready) { cond.wait(lock); } ....//do work } -- Lei(Oddo) Zhang 70 Symonds Street Engineering Science Department

void thread1(){ boost::mutex::scoped_lock lock(mutex);
...//do some work data_ready=true; cond.notify_one() //assume the condition is satisfied. //do more work -------my question is will this part continue? or thread2 will wake up and block thread 1?
}
The thread1() will continue and the thread2() will be still locked. If you wish to avoid this situation, you have to unlock the mutex manually before the notification: lock.unlock(); cond.notify_one(); ...as explained here: http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-...
participants (2)
-
Igor R
-
Lei (oddo) Zhang