Avoid thread locking with boost::condition
Hi, I am trying to avoid thread locking problems when waiting using a boost::condition pseudo code: Main thread -- Start a worker thread -- boost::mutex::scoped_lock lock(m_Mutex); m_Condition.notify_one(); Worker thread boost::mutex::scoped_lock lock(m_Mutex); while (IsAlive()) { // do some work m_Condition.wait(lock); } As we can't predict which thread gets the mutex lock first lets assume that its the main thread. If this happens then m_Condition.wait will wait indefinitely as notify was called before wait. The only reliable way (that I can see) to prevent this is to never use wait, use a timed_wait instead and a loop. Why isn't the notification signal queued in the boost::condition object? Then the order of wait / notification would not be important. thanks Sam
Sam Lievesley wrote: ...
Worker thread
boost::mutex::scoped_lock lock(m_Mutex); while (IsAlive()) {
// do some work
m_Condition.wait(lock); }
...
Main thread
-- Start a worker thread -- boost::mutex::scoped_lock lock(m_Mutex);
make_IsAlive_return_false();
m_Condition.notify_one();
participants (2)
-
Peter Dimov
-
Sam Lievesley