Boost logo

Boost Users :

From: Stéphane Vandenbogaerde (vst_at_[hidden])
Date: 2008-08-21 11:09:38


Hello,

I want to use timed lockable mutex in boost::thread, but the usage is
not so obvious for a beginner.

Would there be anywhere a sample code with this kind og mutex ?

I have a code which works with a boost::mutex in a consumer/producer
queue (code below).
The purpose is to have a maximum waiting delay for waitForNotEmpty().

Thanks.

-------------------------------------------------------------------------

template <typename T>
class MTBornedQueue
{
public:
    ....
    bool waitForNotEmpty()
    {
        if (!m_container.empty())
            return true;

        boost::mutex::scoped_lock lock(m_accessMutex);
        m_notEmpty.wait(lock,
boost::bind(&MTBornedQueue<value_type>::isNotEmpty, this));
        return true;
    }
    ...

private:
    bool isNotEmpty() const { return m_unread > 0; };
    size_type m_unread;
    container_type m_container;
    boost::mutex m_accessMutex;
    boost::condition m_notEmpty;
}


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