title: What if the condition_variable::wait_for delay parameter changes during the wait process?
body: <p>I prefer using <code>condition_variable::wait_for</code> as a timer over chrono based timer since I can override the wait condition by specifying a predicate. So I wrote a test program to check what if the delay duration changes once the wait process has already started. The <code>condition_variable::wait_for</code> seems to ignore the change and instead comes out of the wait process entirely. Why is this? Can I ever change it in the middle of a wait process?</p>
  
  <pre><code>enum STATE {START,STOP,NONE};
  
  STATE  state ;
  condition_variable cv;
  mutex mu;
  chrono::milliseconds delay;
  
  void cv_wait_thread()
  {
      {
          unique_lock&lt;mutex&gt; lock(mu);
          cv.wait(lock, [](){ return state == START; });
      }
  
      chrono::time_point&lt;chrono::high_resolution_clock&gt; start_time = chrono::high_resolution_clock::now();
  
      {
          unique_lock&lt;mutex&gt; lock(mu);
          cv.wait_for(lock, delay);       
      }
  
      auto diff = chrono::duration_cast&lt;chrono::milliseconds&gt;(chrono::high_resolution_clock::now() - start_time);
  
  
      cout &lt;&lt; "Conditional_wait:  "&lt;&lt; diff.count() &lt;&lt; " milliseconds" &lt;&lt; endl;
  
  }
  
  
  void main()
  {
      thread thread1([](){ cv_wait_thread(); });
  
      state = NONE;   
  
      this_thread::sleep_for(chrono::seconds(1));
  
      delay = chrono::milliseconds(3000);
  
      state = START;
      cv.notify_all(); // ask thread to sleep for 3 sec
  
      this_thread::sleep_for(chrono::milliseconds(2000)); // let cv_wait thread start the wait process for at least 2 sec already
  
      delay = chrono::milliseconds(5000); // ask thread to correct and sleep for 5 sec instead
  
      cv.notify_all();
  
      thread1.join(); // thread prints 2000 milli
  }
  </code></pre>