C++ c++;std::打包的任务调用引发系统异常?

C++ c++;std::打包的任务调用引发系统异常?,c++,c++11,asynchronous,exception,packaged-task,C++,C++11,Asynchronous,Exception,Packaged Task,我从cppreference.com获得了一个非常简单的代码片段,如下所示: #include<future> #include<iostream> using namespace std; int main(){ packaged_task<int(int)> t([](int i){return i+1;}); auto f = t.get_future(); cout<<"begin to call t\n";

我从cppreference.com获得了一个非常简单的代码片段,如下所示:

#include<future>
#include<iostream>
using namespace std;
int main(){
    packaged_task<int(int)> t([](int i){return i+1;});
    auto f = t.get_future();
    cout<<"begin to call t\n";
    t(3);
    cout<<f.get()<<endl;
    return 0;
}

那么程序哪里出错了,如何修复它呢?

对不起,我不太明白这一点?这里不能重现错误。在调用f.get()或检查f是否准备就绪之前引入延迟,因为future可能还没有准备就绪。使用
-lpthread
标志编译时,问题消失。经过测试。尝试在不使用
-lpthread
的情况下进行构建,并查看会发生什么。相关感谢此链接,通过使用“-pthread”not“-lpthread”作为链接器标志解决了此问题。
begin to call t
terminate called after throwing an instance of 'std::system_error'
what():  Unknown error -1
Aborted