8
#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 版。

4

1 回答 1

3

GCC 和 GNU STL 不支持完整的 C++ 11。

在这里您可以查看 GCC 和 GNU STL 中的 C++ 11 实现状态:

http://gcc.gnu.org/projects/cxx0x.html

http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html

另外,请阅读此讨论主题:http: //blog.gmane.org/gmane.comp.gcc.bugs/month=20120201

于 2012-09-20T15:09:10.523 回答