[英]In which thread is the terminate handler called?
在哪个线程中称为终止处理程序:
什么时候在noexcept
函数内抛出异常?
当用户调用std::terminate
()时?
在启动或破坏thread
?
它是否在标准中定义,我是否可以访问thread_local
对象?
这个答案总结了评论中给出的答案和现在删除的答案:
它没有在标准中指定( DeiDei ,我也在N4618中检查过)
然而,由于技术原因,不可能在另一个线程中调用处理程序,导致调用std::terminate
( Galik , Hans Passant )
它已在在线编译器( Rinat Veliakhmedov )上得到验证,终止处理程序在线程中调用,导致终止被调用。
您可以使用已删除答案中的此代码自行测试:
#include <string>
#include <exception>
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mutex;
const auto& id = std::this_thread::get_id;
const auto print = [](std::string t){
std::lock_guard<std::mutex> lock(mutex);
std::cout << id() << " " << t << std::endl;
};
void my_terminate_handler(){
print("terminate");
std::abort();
}
void throwNoThrow() noexcept { throw std::exception(); }
void terminator() { std::terminate(); }
int main() {
std::set_terminate(my_terminate_handler);
print("main");
#ifdef CASE1
auto x1 = std::thread(throwNoThrow);
#elif CASE2
auto x1 = std::thread(terminator);
#elif CASE3
auto x1 = std::thread(throwNoThrow);
#endif
x1.join();
}
结论它是未指定的,但似乎总是在线程中调用处理程序,导致调用std::terminate
。 (在gcc-5.4,gcc-7.1,clang-3.8和pthreads上测试 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.