title: How to wait for multiple threads to finish (with c++11 threads)?
body: <p>I'm trying to write a program which uses c++11 threads functionality in order to spawn multiple threads, the main thread must wait for each spawned thread to be finished, and all spawned threads must run in parallel. I've come up with the following approach:   </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;thread&gt;
  #include &lt;condition_variable&gt;
  #include &lt;mutex&gt;
  
  using namespace std;
  
  class Producer
  {
  public:
      Producer(int a_id):
          m_id(a_id),
          m_running(false),
          m_ready(false),
          m_terminate(false)
      {
          m_id = a_id;
          m_thread = thread(&amp;Producer::run, this);
          while (!m_ready) {}
      }
  
      ~Producer() {
          terminate();
          m_thread.join();
      }
  
      void wait() {
          unique_lock&lt;mutex&gt; lock(m_waitForRunFinishMutex);
          m_cond.wait(lock);
          // avoid spurious wake up
          if (m_running) {
              wait();
          }
          lock.unlock();
          cout &lt;&lt; "wait exit " &lt;&lt; m_id &lt;&lt; endl;
      }
  
      void start() {
          m_running = true;
          m_cond.notify_all();
      }
  
      void terminate() {
          start();
          m_terminate = true;
      }
  
      void run() {
          m_ready = true;
          do {
              unique_lock&lt;mutex&gt; lock(m_mutex);
              while (!m_running) {
                  m_cond.wait(lock);
              }
  
              if (!m_terminate) {
                  cout &lt;&lt; "running thread: " &lt;&lt; m_id &lt;&lt; endl;
              }
  
              m_running = false;
              m_cond.notify_all();
          } while (!m_terminate);
      }
  
  private:
      int m_id;
      bool m_running;
      bool m_ready;
      bool m_terminate;
      thread m_thread;
      mutex m_mutex;
      mutex m_waitForRunFinishMutex;
      condition_variable m_cond;
  };
  </code></pre>
  
  <p>The program runs fine when testing with just one thread, i.e the following program:</p>
  
  <pre><code>int main()
  {
      Producer producer1(1);    
      producer1.start();
      producer1.wait();
  
      return 0;
  }
  </code></pre>
  
  <p>Results in the following output:</p>
  
  <pre><code>running thread: 1
  wait exit: 1
  </code></pre>
  
  <p>However if I test the program with 2 thread, e.g:</p>
  
  <pre><code>int main()
  {
      Producer producer1(1);
      Producer producer2(2);
  
      producer1.start();
      producer2.start();
  
      producer1.wait();
      producer2.wait();
  
      return 0;
  }
  </code></pre>
  
  <p>I get the following output:</p>
  
  <pre><code>running thread: 2
  running thread: 1
  wait exit 1
  </code></pre>
  
  <p>It seems producer2 never get notified (in <code>producer2.wait()</code>), and therefore the program never finishes. Hopefully somebody can point out what I'm missing here.</p>
  
  <p><strong>Thanks everybody for the help in addressing the problem. Eventually the root cause of the problem is described in point (3) of the accepted answer. I've solved this by correcting the <code>wait</code> function as follows:</strong></p>
  
  <pre><code>void wait() {
      unique_lock&lt;mutex&gt; lock(m_waitForRunFinishMutex);
      while (m_running) {
          m_cond.wait(lock);
      }
      lock.unlock();
  }
  </code></pre>