Boost logo

Boost Users :

From: Mark Sizer (yg-boost-users_at_[hidden])
Date: 2003-05-22 13:27:30


I have a message queue class that has the following API:

post( msg );
msg get();

where get( msg ) blocks until there is a message available and post( msg
) notifies when a message is posted.

There are four threads waiting in get:
MSG mqueue::get();
{
  MSG msgResult;
  { boost::mutex::scoped_lock lockQueue( _mutexQueue );
    _conditionMessageReady.wait( lockQueue );
    if ( _queueOut.size() > 0 )
    {
      msgResult = _queueOut.front();
      _queueOut.pop();
    }
  }
  return msgResult;
}

The post method USED TO do this:
void mqueue::post( MSG msg )
{
  { boost::mutex::scoped_lock lockQueue( _mutexQueue );
    _queueOut.push(msg);
  }
  _conditionMessageReady.notify_one();
}

When the queue was just a little bit busy (one thread processing, not
all four), messages would go into the queue and never come out.

I changed the .notify_one() to .notify_all() and I no longer have
messages getting stuck in the queue. However, now I have a bunch of
threads getting released when there is nothing for them to do (they just
loop around and wait again, but YUCK!).

Am I missing something or is this "expected" behavior?

Thanks again,
   - Mark

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


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