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.