Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 659
user3623874 Avatar asked Jun 19 '14 07:06

user3623874


2 Answers

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);)

like image 160
Jarod42 Avatar answered Oct 19 '22 21:10

Jarod42


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

like image 2
Chenna V Avatar answered Oct 19 '22 21:10

Chenna V