You can do this:
void XXX::waitForUpdates()
{
boost::unique_lock<boost::mutex> lock(mutex_agentDone);
while(!allAgentUpdatesDone()) {
++waiters;
COND_VAR_AGENT_DONE.wait(lock);
--waiters;
}
}
void XXX::onAgentUpdate(YYY argums){
Agent * target = const_cast<Agent*>(argums.GetAgent());
boost::unique_lock<boost::mutex> lock(mutex_agentDone);
REGISTERED_AGENTS.setDone(target,true);
if (waiters != 0)
COND_VAR_AGENT_DONE.notify_all();
}
The mutex protects the waiters
count. Make sure to set it to zero to start.
You would expect the condition variable to already have something like this, but just the overhead to invoke notify_all
may be significant.
This assumes most of the time there are no waiters. If the issue is that most of the time allAgentUpdatesDone
returns false
, then don't call notify_all
unless all updates are done.