Boost logo

Boost Users :

From: Christopher Currie (Christopher_at_[hidden])
Date: 2003-05-22 14:24:33


Mark,

The problem is that your "get" waits on the condition before it checks
if the condition has already been met. So if you call "get" after the
producer has already "post"ed, you'll sit on the condition forever. The
canonical form of waiting on a condition looks like this:

> MSG mqueue::get();
> {
> MSG msgResult;
> { boost::mutex::scoped_lock lockQueue( _mutexQueue );
> while ( _queueOut.empty() )
> _conditionMessageReady.wait( lockQueue );
>
> msgResult = _queueOut.front();
> _queueOut.pop();
> }
> return msgResult;
> }

Now if you call get and there's something in the queue, there's no need
to wait on the condition.

> P.S. The lockQueue is locked when the condition is done waiting, right?

Yes, the lock is automatically released when entering wait, and acquired
before exiting it.

Hope this helps!

Christopher


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