[英]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.