C++ 11 future_status::deferred not working

青春壹個敷衍的年華 提交于 2019-12-01 01:41:47

问题


#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.


回答1:


GCC and the GNU STL have no support of the complete C++ 11.

Here you can check out the C++ 11 implementation status in GCC and GNU STL:

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

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

Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201



来源:https://stackoverflow.com/questions/12137283/c-11-future-statusdeferred-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!