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