#include <iostream>
#include <future>
#include <chrono>

using namespace std;
using namespace std::chrono;

int sampleFunction(int a)
{
    return a;
}

int main()
{
   future<int> f1=async(launch::deferred,sampleFunction,10);
   future_status statusF1=f1.wait_for(seconds(10));
   if(statusF1==future_status::ready)
        cout<<"Future is ready"<<endl;
   else if (statusF1==future_status::timeout)
        cout<<"Timeout occurred"<<endl;
   else if (statusF1==future_status::deferred)
        cout<<"Task is deferred"<<endl;
   cout<<"Value : "<<f1.get()<<endl;
}

Output -
Timeout occurred
Value : 10

In above example, I was expecting future_status to be deferred instead of timeout. sampleFunction has been launched as launch::deferred. Hence it will not be executed until f1.get() has been called. In such condition wait_for should have returned future_status::deferred and not future_status::timeout.

Appreciate if someone can help me understand this. I am using g++ version 4.7.0 on fedora 17.

Edit
Report