Boost logo

Boost Users :

From: Sam Lievesley (sam.lievesley_at_[hidden])
Date: 2007-08-10 12:46:23


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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net