【发布时间】:2019-07-18 16:59:12
【问题描述】:
在这里挠头。在下面的代码中,f 函数以 3 种不同的方式使用,分别是 task_lambda()、task_bind() 和 task_thread()。然而,在main() 中,只有函数task_lambda() 和task_bind() 被实际调用和执行。但是,如果您敢取消注释 #if 0 代码块,使得未使用的函数 task_thread() 不再在代码中,那么 main 中的代码现在将抛出异常 (-1) system_error。
代码如下:
#include <iostream>
#include <cmath>
#include <thread>
#include <future>
#include <functional>
#include <unistd.h>
// unique function to avoid disambiguating the std::pow overload set
int f(int x, int y) { return std::pow(x,y); }
void task_lambda()
{
std::packaged_task<int(int,int)> task([](int a, int b) {
return std::pow(a, b);
});
std::future<int> result = task.get_future();
task(2, 9);
std::cout << "task_lambda:\t" << result.get() << '\n';
}
void task_bind()
{
std::packaged_task<int()> task(std::bind(f, 2, 11));
std::future<int> result = task.get_future();
task();
std::cout << "task_bind:\t" << result.get() << '\n';
}
//#if 0
void task_thread()
{
std::packaged_task<int(int,int)> task(f);
std::future<int> result = task.get_future();
std::thread task_td(std::move(task), 2, 10);
task_td.detach();
}
//#endif
int main()
{
task_lambda();
task_bind();
sleep(1);
}
这到底是什么意思?
编辑 - 添加工具链信息:
Ubuntu 16.04 - Linux 4.4.0-154-通用
gcc 版本 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.11)
【问题讨论】:
-
有或没有
task_thead,它似乎对我有用。这个问题比代码中显示的要多。尝试干净重建。 -
可能是其他地方的未定义行为只是以一种新的有趣的方式表现出来。
-
哪个编译器?
-
与
-pthread有什么关系。在 G++6.4 下的 https://godbolt.org/ 上崩溃而未通过-pthread。当与-pthread链接时工作正常。 -
@Mgetz 和 Mark B 我认为 rafix07 是对的,添加 -pthread fix my repro(fork,单击右侧的齿轮,额外的编译器标志,-pthread)。
标签: c++