title: Multiple wait using notify all
body: <p>I wish to use condition variable with wait.</p>
  
  <p>I have create a program with 10 threads and each thread wait a signal <code>notify_all()</code> from the main thread. But It goes to deadlock and I don't wish to understand why.</p>
  
  <pre><code>#include &lt;iostream&gt;           // std::cout
  #include &lt;thread&gt;             // std::thread
  #include &lt;mutex&gt;              // std::mutex, std::unique_lock
  #include &lt;condition_variable&gt; // std::condition_variable
  #include &lt;chrono&gt;
  
  using namespace std;
  
  std::mutex mtx;
  std::condition_variable cv;
  int xx = 0;
  
  void print_id (int id) {
      std::unique_lock&lt;std::mutex&gt; lck(mtx);
      cout&lt;&lt;"Start thread id  " &lt;&lt; id &lt;&lt; "  \n";
  
      for(int i=0; i&lt;9; i++)
      {
          cout&lt;&lt;"Thread " &lt;&lt; id &lt;&lt; " i " &lt;&lt; i &lt;&lt;"\n";
          cv.wait(lck);
      }
  }
  
  void go()
  {
      cv.notify_all();
      for(int i=0; i&lt;10; i++)
      {
         //Some works for threads
          cv.notify_all();
      }
  }
  
  int main ()
  {
      std::thread threads[10];
      for (int i=0; i&lt;10; ++i)
          threads[i] = std::thread(print_id,i);
  
      std::cout &lt;&lt; "10 threads ready to race...\n";
      go();                       // go!
  
      for (auto&amp; th : threads) th.join();
  }
  </code></pre>