title: Shutdown boost threads correctly
body: <p>I have x boost threads that work at the same time. One producer thread fills a synchronised queue with calculation tasks. The consumer threads pop out tasks and calculates them. </p>
  
  <p><img src="https://i.stack.imgur.com/YWfzy.png" alt="Synchronised Queue">
  Image Source: <a href="https://www.quantnet.com/threads/c-multithreading-in-boost.10028/" rel="nofollow noreferrer">https://www.quantnet.com/threads/c-multithreading-in-boost.10028/</a></p>
  
  <p>The user may finish the programm during this process, so I need to shutdown my threads properly. My current approach seems to not work, since exceptions are thrown. It's intented that on system shutdown all processes should be killed and stop their current task no matter what they do. Could you please show me, how you would kill thoses threads?</p>
  
  <p>Thread Initialisation: </p>
  
  <pre><code>    for (int i = 0; i &lt; numberOfThreads; i++)
      {
          std::thread* thread = new std::thread(&amp;MyManager::worker, this);
          mThreads.push_back(thread);
      }
  </code></pre>
  
  <p>Thread Destruction:</p>
  
  <pre><code>void MyManager::shutdown()
  {
      for (int i = 0; i &lt; numberOfThreads; i++)
      {
          mThreads.at(i)-&gt;join();
          delete mThreads.at(i);
      }
      mThreads.clear();
  }
  </code></pre>
  
  <p>Worker: </p>
  
  <pre><code>void MyManager::worker()
  {
      while (true)
      {
  
          int current = waitingList.pop();
          Object * p = objects.at(current);
          p-&gt;calculateMesh(); //this task is internally locked by a mutex
  
          try
          {
              boost::this_thread::interruption_point();
          }
          catch (const boost::thread_interrupted&amp;)
          {
              // Thread interruption request received, break the loop
              std::cout &lt;&lt; "- Thread interrupted. Exiting thread." &lt;&lt; std::endl;
              break;
          }
      }
  }
  </code></pre>
  
  <p>Synchronised Queue:</p>
  
  <pre><code>#include &lt;queue&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  
  template &lt;typename T&gt;
  class ThreadSafeQueue
  {
  public:
  
      T pop()
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
          while (queue_.empty())
          {
              cond_.wait(mlock);
          }
          auto item = queue_.front();
          queue_.pop();
  
          return item;
      }
  
      void push(const T&amp; item)
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
          queue_.push(item);
          mlock.unlock();
          cond_.notify_one();
      }
  
  
      int sizeIndicator()
      {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
          return queue_.size();
      }
  
  
  private:
  
      bool isEmpty() {
          std::unique_lock&lt;std::mutex&gt; mlock(mutex_);
          return queue_.empty();
      }
  
      std::queue&lt;T&gt; queue_;
      std::mutex mutex_;
      std::condition_variable cond_;
  };
  </code></pre>
  
  <p>The thrown error call stack: </p>
  
  <pre><code>... std::_Mtx_lockX(_Mtx_internal_imp_t * * _Mtx) Line 68   C++
  ... std::_Mutex_base::lock() Line 42    C++
  ... std::unique_lock&lt;std::mutex&gt;::unique_lock&lt;std::mutex&gt;(std::mutex &amp; _Mtx) Line 220   C++
  ... ThreadSafeQueue&lt;int&gt;::pop() Line 13 C++
  ... MyManager::worker() Zeile 178   C++
  </code></pre>