终止处理程序在哪个线程中调用?
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 进行测试)
相关文章:
- 如何声明由多个线程调用的 C++ DLL 的内部类,而无需导出类
- 非静态成员失败的线程调用函数
- 线程调用的函数对对象删除是否安全?
- 将类成员函数作为线程调用到另一个类成员函数时发出警告消息
- 通过 Qt 中的线程调用 Rust 库
- Qt信号和插槽如果从QRunnable或其他线程调用,则不起作用
- 在销毁期间从另一个线程调用对象上调用方法是否未定义行为?
- 由并发无序映射查找线程调用的函数是否安全?
- 如何从另一个线程调用颤振引擎方法
- C++ std::线程调用方法,从对象原因到调用此类的析构函数
- 从多个线程调用 std::shuffle
- std::thread::join() 可以从非父线程调用吗?
- 如何使用 Win32 API 从 Excel VBA 中的非"Single-Threaded Apartment"线程调用 InternetGetProxyInfo
- 带参数的线程调用库函数
- 如何在本机C++中跨线程调用(在主线程上回调)
- 如果两个线程调用同一个函数,但函数中的所有变量都是局部变量,我还需要担心线程之间共享数据吗?
- 优雅断言函数不是从多个线程调用的
- 从第二个线程调用Qt信号有效 - >对连接的插槽没有影响
- 包装标准::线程调用函数
- 从 Visual C++ 2010 中的单独线程调用消息框