title: notifyall not working in C++ multithreading . Causing deadlock
body: <pre><code>#include &lt;iostream&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  #include &lt;thread&gt;
  using namespace std;
  int num = 1;
  #define NUM 20
  condition_variable odd;
  condition_variable even;
  mutex mut;
  void thread_odd()
  {
          while(num &lt; NUM -1)
          {
              if(num%2 != 1)
              {
                    unique_lock&lt;mutex&gt; lock(mut);
                    odd.wait(lock);
              }
              cout&lt;&lt;"ODD : "&lt;&lt;num&lt;&lt;endl;
              num++;
              even.notify_all();  // Line X
          }
  }
  void thread_even()
  {
          while(num &lt; NUM )
          {
              if(num%2 != 0)
              {
                    unique_lock&lt;mutex&gt; lock(mut);
                    even.wait(lock);
              }
              cout&lt;&lt;"EVEN : "&lt;&lt;num&lt;&lt;endl;
              num++;
              odd.notify_all();
          }
  }
  int main()
  {
         thread t1(thread_odd), t2(thread_even);
         t1.join();
         t2.join();
         return 0;
  }
  </code></pre>
  
  <p>/*   Above is the program to print ODD &amp; EVEN numbers in synchronized manner ( one by one ) . The code is working fine most of the time . 
              But it is getting into a deadlock situation sometimes . 
              That is happening when odd thread is hitting notify_all but before the even thread wakes up it odd thread acquires the lock and then as it finds wait condition it goes into wait while the even thread hasn't wake up . 
              Leaving a dealock situation . I tried replacing notify_all to notify_one , 
              but the problem still persists . Is there any change in the design required ? 
              Or is there anything which I am missing completely ? */</p>