Race in boost::condition_variable::wait
Hi, Regarding boost::condition_variable::wait (listed below with an obvious modification), It seems to me that there is a race condition because the following operations are not atomic: 1) unlocking of "m" 2) waiting on "cond". Can someone explain to me why boost::condition_variable::wait is not racing the user code in the example below? Thank you, Chris === inline void condition_variable::wait(unique_lock<mutex>& m) { int res=0; { thread_cv_detail::lock_on_exit<unique_lock<mutex> > guard; detail::interruption_checker check_for_interruption(&internal_mutex,&cond); guard.activate(m); #ifdef ENCOURAGE_DEADLOCK boost::this_thread::sleep_for(boost::chrono::seconds(100)); #endif do { res = pthread_cond_wait(&cond,&internal_mutex); } while (res == EINTR); } } === #define ENCOURAGE_DEADLOCK #include <boost/thread.hpp> int main() { boost::mutex Mutex; boost::condition_variable Condition; bool Quit = false; boost::thread Thread([&] { boost::unique_lock<boost::mutex> Lock(Mutex); while (!Quit) { Condition.wait(Lock); } }); boost::this_thread::sleep_for(boost::chrono::seconds(1)); boost::lock_guard<boost::mutex> Lock(Mutex); Quit = true; Condition.notify_all(); return 0; }
participants (1)
-
Chris Stankevitz