title: Is this a safe way to use a condition variable without mutex
body: <p>My current code looks like this</p>
  
  <pre><code>void XXX::waitForUpdates()
  {
      boost::unique_lock&lt;boost::mutex&gt; lock(mutex_agentDone);
      while(!allAgentUpdatesDone()) {
          COND_VAR_AGENT_DONE.wait(lock);
      }
  }
  
  void XXX::onAgentUpdate(YYY argums){
      Agent * target = const_cast&lt;Agent*&gt;(argums.GetAgent());
      boost::unique_lock&lt;boost::mutex&gt; lock(mutex_agentDone);
      REGISTERED_AGENTS.setDone(target,true);
          COND_VAR_AGENT_DONE.notify_all();
  }
  </code></pre>
  
  <p>everything is fine, except when <code>onAgentUpdate</code> is called about a million times every 1 second, I have to worry about performance and optimization.</p>
  
  <p>So I figured if I change the <code>wait(lock)</code> to <code>timed_wait</code> version that does the <code>allAgentUpdatesDone()</code> check, I can skip <code>.notify()</code>s which is otherwise invoked in the order of hundreds of thousands every second! Don't gasp, this is a simulation framework :)</p>
  
  <p>Then I asked myseld:  what do I need the <code>mutex_agentDone</code> for? I can modify the two functions like this:</p>
  
  <pre><code>void XXX::waitForUpdates()
  {
      //this lock will become practically useless, coz there is no other 
      // mutex_agentDone being locked in any other function.
      boost::unique_lock&lt;boost::mutex&gt; lock(mutex_agentDone);
      while(!allAgentUpdatesDone()) {     
          COND_VAR_AGENT_DONE.timed_wait(lock,some_time_interval);
      }
  }
  
  void XXX::onAgentUpdate(YYY argums){
      Agent * target = const_cast&lt;Agent*&gt;(argums.GetAgent());
      REGISTERED_AGENTS.setDone(target,true)
  }
  </code></pre>
  
  <p>The question is: is this safe?</p>
  
  <p>thank you</p>
  
  <p>A small note:
  Assume that the rest of operations in the two functions are already safeguarded by their own mutex (<code>REGISTERED_AGENTS</code> is a class object having a <code>container</code> and its own <code>mutex</code> being invoked in every accessor and iteration method,  so <code>allAgentUpdatesDone()</code> is iterating through the same <code>container</code> using same <code>mutex</code> as REGISTERED_AGENTS)</p>