title: detached thread crashing on exiting
body: <p>I am using a simple thread pool as below-</p>
  
  <pre><code>template&lt;typename T&gt;
  class thread_safe_queue // thread safe worker queue.
  {
  private:
      std::atomic&lt;bool&gt; finish;
      mutable std::mutex mut;
      std::queue&lt;T&gt; data_queue;
      std::condition_variable data_cond;
  public:
      thread_safe_queue() : finish{ false }
      {}
  
      ~thread_safe_queue()
      {}
  
      void setDone()
      {
          finish.store(true);
          data_cond.notify_one();
      }
      void push(T new_value)
      {
          std::lock_guard&lt;std::mutex&gt; lk(mut);
          data_queue.push(std::move(new_value));
          data_cond.notify_one();
      }
  
      void wait_and_pop(T&amp; value)
      {
          std::unique_lock&lt;std::mutex&gt; lk(mut);
          data_cond.wait(lk, [this]
          {
              return false == data_queue.empty();
          });
          if (finish.load() == true)
              return;
          value = std::move(data_queue.front());
          data_queue.pop();
      }
      bool empty() const
      {
          std::lock_guard&lt;std::mutex&gt; lk(mut);
          return data_queue.empty();
      }
  };
  
  //Thread Pool
  class ThreadPool
  {
  private:
      std::atomic&lt;bool&gt; done;
      unsigned thread_count;
      std::vector&lt;std::thread&gt; threads;
  
  public:
      explicit ThreadPool(unsigned count = 1);
  
      ThreadPool(const ThreadPool &amp; other) = delete;
      ThreadPool&amp; operator = (const ThreadPool &amp; other) = delete;
  
      ~ThreadPool()
      {
          done.store(true);
          work_queue.setDone();
          // IF thread is NOT marked detached and this is uncommented the worker threads waits infinitely.
          //for (auto &amp;th : threads)
          //{
           //     if (th.joinable())
           //     th.join();
          // }
      }
  
      void init()
      {
          try
          {
              thread_count = std::min(thread_count, std::thread::hardware_concurrency());
              for (unsigned i = 0; i &lt; thread_count; ++i)
              {
                  threads.emplace_back(std::move(std::thread(&amp;ThreadPool::workerThread, this)));
                  threads.back().detach();
                // here the problem is if i dont mark it detatched thread infinitely waits for condition.
               // if i comment out the detach line and uncomment out comment lines in ~ThreadPool main threads waits infinitely.
              }
          }
          catch (...)
          {
              done.store(true);
              throw;
          }
      }
  
      void workerThread()
      {
          while (true)
          {
              std::function&lt;void()&gt; task;
              work_queue.wait_and_pop(task);
              if (done == true)
                  break;
              task();
          }
      }
      void submit(std::function&lt;void(void)&gt; fn)
      {
          work_queue.push(fn);
      }
  };
  </code></pre>
  
  <p>The usage is like :</p>
  
  <pre><code>struct start
  {
  public:
      ThreadPool::ThreadPool m_NotifPool;
      ThreadPool::ThreadPool m_SnapPool;
      start()
      {
          m_NotifPool.init();
          m_SnapPool.init();
      }
  };    
  int main()
  {
      start s;
      return 0;
  }    
  </code></pre>
  
  <p>I am running this code on visual studio 2013. The problem is when main thread exits. The program crashes. It throws exception.
  Please help me with what am i doing wrong? How do i stop the worker thread properly? I have spent quite some time but still figuring out what is the issue.</p>
  
  <p>Thanks for your help in advance.</p>