tsh*_*h06 8 c++ multithreading stl future c++11
#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
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我期待future_status的是deferred代替timeout.sampleFunction已发布为launch::deferred.因此,f1.get()在被调用之前不会执行.在这种情况下wait_for应该返回future_status::deferred而不是future_status::timeout.
感谢有人能帮助我理解这一点.我在fedora 17上使用g ++版本4.7.0.
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
| 归档时间: |
|
| 查看次数: |
891 次 |
| 最近记录: |