【发布时间】:2022-06-27 23:31:30
【问题描述】:
我有这个代码 sn-p:
#include<future>
#include<iostream>
using namespace std;
int main() {
cout << "---------" << endl;
packaged_task<int(int, int)> task([](int a, int b){
cout << "task thread\n";
return a + b;
});
thread tpt(move(task), 3, 4);
cout << "after thread creation\n";
future<int> sum = task.get_future();
cout << "before join\n";
tpt.join();
cout << "after join\n";
sum.wait();
cout << "after wait\n";
cout << sum.get() << endl;
return 0;
}
它只是打印出来的
---------
after thread creation
task thread
然后挂起大约 2 秒,然后结束。我没有看到我的 packaged_task 函数执行。它没有打印"after join\n" 和"after wait\n"
为什么我的程序意外结束,如何解决?
【问题讨论】:
标签: c++ multithreading future packaged-task