std::thread 导致程序中止
std::thread causes program to abort
我有以下代码片段:
#include <thread>
int main(){
std::thread trial([](){ return 2;});
//trial.join()
return 0;
}
由此我得到以下输出:
terminate called without an active exception
[1] 17963 abort (core dumped) ./a.out
现在,当我在创建线程后调用 .join()
时不会发生这种情况。据我所知,.join()
一直等到线程执行结束。但是,它似乎也可以防止中止的发生。有人能解释一下这是怎么回事吗?
Could somebody explain what's going on?
来自std::thread
的析构函数文档:
If *this has an associated thread (joinable() == true), std::terminate()
is called.
在示例中,您加入线程失败,因此在销毁时可以加入,因此调用进程std::terminate()
。默认情况下 std::terminate()
调用 std::abort
.
如果加入,加入后线程将不可加入。因此 std::terminate()
不会在销毁时被调用。
我有以下代码片段:
#include <thread>
int main(){
std::thread trial([](){ return 2;});
//trial.join()
return 0;
}
由此我得到以下输出:
terminate called without an active exception
[1] 17963 abort (core dumped) ./a.out
现在,当我在创建线程后调用 .join()
时不会发生这种情况。据我所知,.join()
一直等到线程执行结束。但是,它似乎也可以防止中止的发生。有人能解释一下这是怎么回事吗?
Could somebody explain what's going on?
来自std::thread
的析构函数文档:
If *this has an associated thread (joinable() == true),
std::terminate()
is called.
在示例中,您加入线程失败,因此在销毁时可以加入,因此调用进程std::terminate()
。默认情况下 std::terminate()
调用 std::abort
.
如果加入,加入后线程将不可加入。因此 std::terminate()
不会在销毁时被调用。