【问题标题】:C++ 11 future_status::deferred not workingC++ 11 future_status::deferred 不工作
【发布时间】:2012-08-27 06:40:46
【问题描述】:
#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_statusdeferred 而不是 timeoutsampleFunction 已作为 launch::deferred 启动。因此在调用f1.get() 之前它不会被执行。在这种情况下,wait_for 应该返回 future_status::deferred 而不是 future_status::timeout

感谢有人可以帮助我理解这一点。 我在 Fedora 17 上使用 g++ 4.7.0 版。

【问题讨论】:

  • GCC 和提供的标准库还没有完全实现 C++11 的所有功能。参见例如here 了解图书馆的状态。
  • 来自 Joachim 引用的页面:“类模板未来:部分:定时等待函数不返回 future_status”。说明一切
  • 投票结束,因为问题没有解决方案。

标签: c++ multithreading stl c++11 future


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多