Boost logo

Threads-Devel :

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


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



Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk