title: Does notify happen-before wakeup of wait?
body: <p>Is this program</p>
  
  <pre><code>std::condition_variable cond_var;
  std::mutex mtx;
  std::atomic&lt;int&gt; value {0};
  
  const auto mem_ord = std::memory_order_seq_cst;
  
  std::thread notify_thread([&amp;]
      {
          {
              //std::unique_lock&lt;std::mutex&gt; lock (mtx);
              value.store (1, mem_ord);
          }
          cond_var.notify ();
      });
  
  {
      std::unique_lock&lt;std::mutex&gt; lock (mtx);
      cond_var.wait (lock, [&amp;] { return value.load (mem_ord) != 0; });
  }
  
  notify_thread.join ();
  </code></pre>
  
  <p>guaranteed to terminate or may it deadlock? What if one would use <code>mem_ord = std::memory_order_relaxed</code>? May there be any other reasons to hold a lock while <code>store</code>-ing into the <code>atomic&lt;int&gt;</code>? <a href="http://en.cppreference.com/w/cpp/thread/condition_variable" rel="nofollow">cppreference.com</a> demands that one has to uncomment the line with the lock-guard in the <code>notify_thread</code>.</p>
  
  <p>I would expect that (with any memory order) the <code>store</code> <em>happens-before</em> the notify, which <em>happens-before</em> the (potential) wakeup in the <code>wait</code>, which <em>happens-before</em> the <code>load</code>. (I say "potential wakeup" because the <code>notify-thread</code> may, of course, finish before the <code>wait</code> on the condition-variable in which case there never will be a wakeup). So, I would assume that (with any memory order) there is no need to uncomment the line with the lock-guard.</p>