title: Producer Consumer Thread with shutdown c++
body: <p>Hi quick question given the following c++11 code which works fine for producer/consumer, the problem is I want to shut the DataQueue down and stop all consumers. Although there in lies the issue as the problem is the consumers just call popWait() and can be blocked.  How can I shutdown my consumers in this case? This is probably a design issue that needs to be rectified. I am trying not to incur any performance hits as this code should ideally be using a disruptor pattern or similar to make the queue lock free. Being that as it may I was wondering if there was something simple to have the consumers know to stop calling the pop wait function when the producer shuts down. The tricky part is shutting down if there is still data on the queue you would have to wait to have the consumers finish pulling off the data. I believe I have a solution where the consumer has it's own shutdown but open to ideas. Thanks in advance.</p>
  
  <pre><code>#ifndef __DataQueue_h__
  #define __DataQueue_h__
  
  #include &lt;mutex&gt;
  #include &lt;queue&gt;
  #include &lt;condition_variable&gt;
  #include &lt;chrono&gt;
  
  template &lt;typename DataT&gt;
  class DataQueue
  {
    public:
  
    DataQueue (): _shutdown(false), _waitTime(5), _itemAvailable() {}
  
    void push ( const DataT&amp; data )       
    {
      std::unique_lock&lt;std::mutex&gt; lock(_mutex);
      queue.push(data);
      _itemAvailable.notify_one();
    }
  
  // worked fine until I need to shutdown services... then some were blocked
    DataT popWait()
    {
      std::unique_lock&lt;std::mutex&gt; lock(_mutex);
  
      if(queue.empty())
      {
        _itemAvailable.wait(lock);
      }
  
      DataT temp(queue.front());
      queue.pop();
  
      return temp;
    }
  
    inline void shutdown()
    {
      _shutdown = true;
    }
  
    private:
    std::queue&lt;DataT&gt; queue;
    bool _shutdown;
    unsigned int _waitTime;
    std::mutex _mutex;
    std::condition_variable _itemAvailable;
  
  };
  
  #endif
  </code></pre>