title: Shared Queue in C++
body: <p>I just simply get packets from network, and Enqueue them in one thread and then consume this packets (Dequeue) in an other thread.</p>
  
  <p>So i decide to use boost library to make a shared queue  based on
  <a href="https://www.quantnet.com/cplusplus-multithreading-boost/" rel="nofollow">https://www.quantnet.com/cplusplus-multithreading-boost/</a></p>
  
  <pre><code>template &lt;typename T&gt;
  class SynchronisedQueue
  {
  private:
      std::queue&lt;T&gt; m_queue;  // Use STL queue to store data
      boost::mutex m_mutex;   // The mutex to synchronise on
      boost::condition_variable m_cond;// The condition to wait for
  
  public:
  
      // Add data to the queue and notify others
      void Enqueue(const T&amp; data)
      {
          // Acquire lock on the queue
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
  
          // Add the data to the queue
          m_queue.push(data);
  
          // Notify others that data is ready
          m_cond.notify_one();
  
      } // Lock is automatically released here
  
      // Get data from the queue. Wait for data if not available
      T Dequeue()
      {
  
          // Acquire lock on the queue
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
  
          // When there is no data, wait till someone fills it.
          // Lock is automatically released in the wait and obtained 
          // again after the wait
          while (m_queue.size()==0) m_cond.wait(lock);
  
          // Retrieve the data from the queue
          T result=m_queue.front(); m_queue.pop();
          return result;
  
      } // Lock is automatically released here
  };
  </code></pre>
  
  <blockquote>
    <p>The problem is , while not getting any data, Dequeue() method
    blocks my consumer thread, and when i want to terminate consumer
    thread i can not able to end it or stop it sometimes.</p>
  </blockquote>
  
  <p>What is the suggested way to end blocking of Dequeue(), so that i can safely terminate the thread that consume packets?
  Any ideas suggestions?</p>
  
  <p>PS: The site  <a href="https://www.quantnet.com/cplusplus-multithreading-boost/" rel="nofollow">https://www.quantnet.com/cplusplus-multithreading-boost/</a>  use "boost::this_thread::interruption_point();"  for stopping consumer thread ... Because of my legacy code structure this is not possible for me...</p>
  
  <p><strong>Based on Answer I update Shared Queue like this:</strong></p>
  
  <pre><code>#include &lt;queue&gt;
   #include &lt;boost/thread.hpp&gt;  
  
  template &lt;typename T&gt;
  class SynchronisedQueue
  {
  public:
  
      SynchronisedQueue()
      {
          RequestToEnd = false;  
          EnqueueData = true;
      }
      void Enqueue(const T&amp; data)
      {
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
  
          if(EnqueueData)
          {
              m_queue.push(data);
              m_cond.notify_one();
          }
  
      } 
  
  
      bool TryDequeue(T&amp; result)
      {
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
  
          while (m_queue.empty() &amp;&amp; (! RequestToEnd)) 
          { 
              m_cond.wait(lock);
          }
  
          if( RequestToEnd )
          {
               DoEndActions();
               return false;
          }
  
          result= m_queue.front(); m_queue.pop();
  
          return true;
      }
  
      void StopQueue()
      {
          RequestToEnd =  true;
          Enqueue(NULL);        
      }
  
      int Size()
      {
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
          return m_queue.size();
  
      }
  
  private:
  
      void DoEndActions()
      {
          EnqueueData = false;
  
          while (!m_queue.empty())  
          {
              m_queue.pop();
          }
      }
  
  
  
      std::queue&lt;T&gt; m_queue;              // Use STL queue to store data
      boost::mutex m_mutex;               // The mutex to synchronise on
      boost::condition_variable m_cond;            // The condition to wait for
  
      bool RequestToEnd;
      bool EnqueueData;
  };
  </code></pre>
  
  <p><strong>And Here is my Test Drive:</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  #include "SynchronisedQueue.h"
  
  using namespace std;
  
  SynchronisedQueue&lt;int&gt; MyQueue;
  
  void InsertToQueue()
  {
      int i= 0;
  
      while(true)
      {
          MyQueue.Enqueue(++i);
      }
  
  }
  
  void ConsumeFromQueue()
  {
      while(true)
      {
          int number;
  
          cout &lt;&lt; "Now try to dequeue" &lt;&lt; endl;
  
          bool success = MyQueue.TryDequeue(number);
  
          if(success)
          {
  
              cout &lt;&lt; "value is " &lt;&lt; number &lt;&lt; endl;
  
          }
  
          else
          {
              cout &lt;&lt; " queue is stopped" &lt;&lt; endl;
              break;
  
          }
      }
  
  
      cout &lt;&lt; "Que size is : " &lt;&lt; MyQueue.Size() &lt;&lt;  endl;
  }
  
  
  
  int main()
  {
  
      cout &lt;&lt; "Test Started" &lt;&lt; endl;
  
      boost::thread startInsertIntoQueue = boost::thread(InsertToQueue);
      boost::thread consumeFromQueue = boost::thread(ConsumeFromQueue);
  
      boost::this_thread::sleep(boost::posix_time::seconds(5)); //After 5 seconds
  
      MyQueue.StopQueue();
  
      int endMain;
  
      cin &gt;&gt; endMain;
  
  
      return 0;
  }
  </code></pre>
  
  <p><strong>For now it seems to work...Based on new suggestions:</strong></p>
  
  <p>i  change Stop Method as:</p>
  
  <pre><code>void StopQueue()
      {
          boost::unique_lock&lt;boost::mutex&gt; lock(m_mutex);
          RequestToEnd =  true;
          m_cond.notify_one();          
      }
  </code></pre>