问题描述
#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
.
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
.
感谢有人可以帮助我理解这一点.我在fedora 17上使用的是g ++版本4.7.0.
Appreciate if someone can help me understand this.I am using g++ version 4.7.0 on fedora 17.
推荐答案
GCC和GNU STL不支持完整的C ++11.
GCC and the GNU STL have no support of the complete C++ 11.
您可以在此处查看GCC和GNU STL中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
此外,请阅读此讨论主题: http://blog.gmane .org/gmane.comp.gcc.bugs/month = 20120201
Also, read this discussion thread: http://blog.gmane.org/gmane.comp.gcc.bugs/month=20120201
这篇关于C ++ 11 future_status :: deferred无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!