title: C++11 future.wait_for() always returns future_status::timeout
body: <p>I have a C++11 program that checks whether a number is prime. There is a future object that the program waits for to be ready. After it is ready, the program tells whether a a provider function of future object considered the number to be prime.</p>
  
  <pre><code>// future example
  #include &lt;iostream&gt;       // std::cout
  #include &lt;future&gt;         // std::async, std::future
  #include &lt;chrono&gt;         // std::chrono::milliseconds
  
  
  const int number = 4; // 444444443
  
  // a non-optimized way of checking for prime numbers:
  bool is_prime (int x) {
      for (int i=2; i&lt;x; ++i) if (x%i==0) return false;
        return true;
      }
  
  int main ()
  {
      // call function asynchronously:
      std::future&lt;bool&gt; fut = std::async (is_prime, number); 
  
      // do something while waiting for function to set future:
      std::cout &lt;&lt; "checking, please wait";
      std::chrono::milliseconds span (100);
      //std::chrono::duration&lt;int&gt; span (1);
  
      while (fut.wait_for(span)==std::future_status::timeout) {
          std::cout &lt;&lt; '.';
          std::cout.flush();
      }
  
      bool x = fut.get();     // retrieve return value
  
      std::cout &lt;&lt; "\n"&lt;&lt;number&lt;&lt;" " &lt;&lt; (x?"is":"is not") &lt;&lt; " prime.\n";
  
      return 0;
  }
  </code></pre>
  
  <p>If you run the program, you will see that it is in an infinite while loop, since <code>wait_for()</code> always returns <code>future_status::timeout</code>, which means that the shared state is never ready. What is the reason for that? I took this program from <a href="http://www.cplusplus.com/reference/future/future/wait_for/" rel="noreferrer">http://www.cplusplus.com/reference/future/future/wait_for/</a>, so I expected it to work. However, if I comment out the while loop, the program will work fine.</p>