C++11 future.wait_for() always returns future_status::timeout
Asked Answered
H

2

6

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.

// future example
#include <iostream>       // std::cout
#include <future>         // std::async, std::future
#include <chrono>         // 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<x; ++i) if (x%i==0) return false;
      return true;
    }

int main ()
{
    // call function asynchronously:
    std::future<bool> fut = std::async (is_prime, number); 

    // do something while waiting for function to set future:
    std::cout << "checking, please wait";
    std::chrono::milliseconds span (100);
    //std::chrono::duration<int> span (1);

    while (fut.wait_for(span)==std::future_status::timeout) {
        std::cout << '.';
        std::cout.flush();
    }

    bool x = fut.get();     // retrieve return value

    std::cout << "\n"<<number<<" " << (x?"is":"is not") << " prime.\n";

    return 0;
}

If you run the program, you will see that it is in an infinite while loop, since wait_for() always returns future_status::timeout, which means that the shared state is never ready. What is the reason for that? I took this program from http://www.cplusplus.com/reference/future/future/wait_for/, so I expected it to work. However, if I comment out the while loop, the program will work fine.

Heber answered 19/6, 2014 at 7:26 Comment(6)
Since it could be important (C++11 implementations not necessarily being completely stable yet): Mind telling us what platform you are working on?Epispastic
I was getting the same behavior while using Ubuntu 13.04 and Cygwin 1.7.29 . It seems like the problem was that with my compiler the default launching policy chosen was std::launch::deferred rather than std::launch::async.Heber
It is clearly a bug in compiler, as with deferred function, wait_for should return future_status::deferred anyway.Anion
MSVC2013 has this bug as well. Wonderful.Unfit
had this problem with g++ 4.8.4, upgraded to g++ 4.9.3, now wait_for doesn't wait, and returns future_status::deferred all the time insteadPoling
Jarod42's suggestion to set the policy explicitly to std::launc::async works in either g++ version though.Poling
A
12

The code is working: (g++ 4.9, clang 3.4) http://coliru.stacked-crooked.com/a/f3c2530c96591724

I get the same behavior as you with MINGW32 with g++ 4.8.1 though. Setting the policy explicitly to std::launch::async solves the issue.
(i.e: std::async(std::launch::async, is_prime, number);)

Anion answered 19/6, 2014 at 7:50 Comment(0)
S
2

Not sure if this is a bug in compiler but I believe wait_for should be returning future_status::deferred This is exactly what Scott Meyers discusses in his book Effective C++ in Item 36:Specify std::launch::async if asynchronicity is essential

And the solution he proposes

The fix is simple: just check the future corresponding to the std::async call to see whether the task is deferred, and, if so, avoid entering the timeout-based loop. Unfortunately, there’s no direct way to ask a future whether its task is deferred. Instead, you have to call a timeout-based function—a function such as wait_for. In this case, you don’t really want to wait for anything, you just want to see if the return value is std::future_status::deferred, so stifle your mild disbelief at the necessary circumlocution and call wait_for with a zero timeout.

In your case, you can either be explicit about asynchronicity as @Jarod mentioned in his solution i.e. using std::launch::async Or you can rewrite your code as below

bool x;

// if task is deferred...
if (fut.wait_for(std::chrono::milliseconds(0)) == std::future_status::deferred)
{
  // ...use wait or get on fut
  // to call is_prime synchronously
  x = fut.get();     // retrieve return value
}
else
{
  // task isn't deferred
  // infinite loop not possible (assuming is_prime finishes)
  while (fut.wait_for(span) != std::future_status::ready) { 
    // task is neither deferred nor ready,
    // so do concurrent work until it's ready
    std::cout << '.';
    std::cout.flush();
  }

  // fut is ready
  x = fut.get();     // retrieve return value
}

Test it

Shibboleth answered 24/7, 2017 at 21:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.