Boost logo

Boost Users :

From: Amerio (amerio_at_[hidden])
Date: 2004-12-23 03:58:40


> > void post()
> > {
> > boost::mutex::scoped_lock lock(m_mutex);
> > ++m_count;
> > m_cond_.notify_one();
> > }
> >
> > Objective : I need a semaphore to sync many readers to a single input
> > queue.
> > If you know another implementation or solution to this specific
> > problem, your help is welcome.
>
> Looks almost correct. But as you likely will have multiple waiters, I
> suggest using m_cond_.notify_all() instead.

This one I don't understand. When my sem is incremented by one, I want to
wake up only one reader (as there is only one new entry to be read). Why
would I want to wake up all of them ?
As I see it, one post means one new entry exactly.

> I also recommend to look at the "condvar" example of boost.thread, which
> shows a simple queue implementation (albeit for single reader/writer).

I find their example a little tricky. I specially miss a multiple
reader/writer example.
Moreover, the loop around the cond.wait() is tricky at first and should get
more attention.
In my own example, I was also wondering if I got the "while" loop right :
    void wait()
    {
        boost::mutex::scoped_lock lock(m_mutex);
        while ( m_count==0 ) m_cond.wait( lock ); // is cond test ok ?
        --m_count;
    }

Thanks for your insights.


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