title: Condition variables and lockfree container
body: <p>Conditional variables use a mutex and the .wait() function unlocks the
  mutex so another thread can access the shared data. When the condition
  variable is notified it tries to lock the mutex again to use the shared
  data.</p>
  <p>This pattern is used in the following concurrent_queue example from <a href="http://www.justsoftwaresolutions.co.uk/threading/implementing-a-thread-safe-queue-using-condition-variables.html" rel="nofollow noreferrer">Anthony Williams</a>:</p>
  <pre><code>template&lt;typename Data&gt;
  class concurrent_queue
  {
  private:
      boost::condition_variable the_condition_variable;
  public:
      void wait_for_data()
      {
          boost::mutex::scoped_lock lock(the_mutex);
          while(the_queue.empty())
          {
              the_condition_variable.wait(lock);
          }
      }
      void push(Data const&amp; data)
      {
          boost::mutex::scoped_lock lock(the_mutex);
          bool const was_empty=the_queue.empty();
          the_queue.push(data);
          if(was_empty)
          {
              the_condition_variable.notify_one();
          }
      }
  };
  </code></pre>
  <p>Since the code uses std::queue it's clear that the mutex has to be
  locked when accessing the queue.
  But let's say instead of std::queue one uses <a href="http://msdn.microsoft.com/en-us/library/ee355358.aspx" rel="nofollow noreferrer">Microsofts
  Concurrency::concurrent_queue</a> from PPL. Member functions like empty,
  push and try_pop are thread safe. Do I still need to lock a mutex in
  this case or can the condition variable be used like this, without
  creating any possible race conditions.</p>
  <p>My code (that seems to work, but what does that mean in multithreading?) looks like this. I have one producer that pushes items into Microsofts concurrent_queue and one background thread that waits for new items in this queue.</p>
  <p>The consumer/background thread:</p>
  <pre><code>while(runFlag) //atomic
  {
      while(the_queue.empty() &amp;&amp; runFlag) //wait only when thread should still run
      {
          boost::mutex mtx; //local mutex thats locked afterwards. awkward.
          boost::mutex::scoped_lock lock(mtx);
          condition.wait(lock);
      }
  
      Data d;
      while(!the_queue.empty() &amp;&amp; the_queue.try_pop(d))
      {
         //process data
      }
  }
  </code></pre>
  <p>The producer/main thread:</p>
  <pre><code>const bool was_empty = the_queue.empty();
  Data d; 
  the_queue.push(d);
  if(was_empty) cond_var.notify_one();
  </code></pre>
  <p>The shutdown procedure:</p>
  <pre><code>bool expected_run_state = true;
  if(runLoop.compare_exchange_strong(expected_run_state, false))
  {
      //atomically set our loop flag to false and 
      //notify all clients of the queue to wake up and exit
      cond_var.notify_all();
  }
  </code></pre>
  <p>As said above this code seems to work but that doesn't necessarily mean it's correct. Especially the local mutex that is only used because the condition variable interface forces me to use a mutex, seems like a very bad idea. I wanted to use condition variables since the time between data items added to the queue hard to predict and I would have to create to sleep and wake up periodically like this:</p>
  <pre><code>if(the_queue.empty()) Sleep(short_amount_of_time);
  </code></pre>
  <p>Are there any other, maybe OS (in my case: Windows) specific tools, that make a background thread sleep until some condition is met without regularly waking up and checking the condition?</p>