C++ 11 future_status::d推迟不起作用

C++ 11 future_status::deferred not working

本文关键字:不起作用 推迟 status future C++      更新时间:2023-10-16
#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 4.7.0 上使用 g++ 版本 17。

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