#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
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我期待future_status的是deferred代替timeout.sampleFunction已发布为launch::deferred.因此,f1.get()在被调用之前不会执行.在这种情况下wait_for应该返回future_status::deferred而不是future_status::timeout.
感谢有人能帮助我理解这一点.我在fedora 17上使用g ++版本4.7.0.