title: conditional_variable "spurious" awakenings
body: <p>I'm having trouble understanding <code>conditional_variable</code> <a href="http://en.cppreference.com/w/cpp/thread/condition_variable/wait_for" rel="nofollow noreferrer">wait_for</a>.</p>
  
  <p>Sample program:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  #include &lt;condition_variable&gt;
  #include &lt;chrono&gt;
  
  std::mutex m;
  std::condition_variable cv;
  bool ready = false;
  
  using namespace std;
  
  void worker_thread()
  {
      std::unique_lock&lt;std::mutex&gt; lk(m);
  
      for (int i=0; i&lt;3; i++) {
        auto remaining = chrono::milliseconds::max();
  
        std::cout &lt;&lt; "Going to sleep for " &lt;&lt; remaining.count() &lt;&lt; endl;
        bool r = cv.wait_for(lk, remaining, [] () { return ready;});
        std::cout &lt;&lt; "Wakey wakey " &lt;&lt; r &lt;&lt; endl;
      }
  
      std::cout &lt;&lt; "Thread finished" &lt;&lt; endl;
  }
  
  int main()
  {
      std::thread worker(worker_thread);
      worker.join();
  }
  </code></pre>
  
  <p>What I expect this program to do:</p>
  
  <ul>
  <li>Go to sleep forever since I never set <code>ready</code> to true nor called <code>cv.notify_one()</code></li>
  </ul>
  
  <p>What it actually does:</p>
  
  <pre><code>Going to sleep for 9223372036854775807
  Wakey wakey 0
  Going to sleep for 9223372036854775807
  Wakey wakey 0
  Going to sleep for 9223372036854775807
  Wakey wakey 0
  Thread finished
  </code></pre>
  
  <p>Is this because of "spurious awakenings"? But I used overload <code>(2)</code> which says in the documentation: <code>This overload may be used to ignore spurious awakenings.</code></p>
  
  <p>Execute code here: <a href="https://repl.it/repls/EnchantedFatalUnderstanding" rel="nofollow noreferrer">https://repl.it/repls/EnchantedFatalUnderstanding</a></p>