10
在哪個線程被稱爲終止處理程序:在哪個線程中調用了終止處理程序?
當一個例外是
noexcept
函數內拋出?當用戶撥打
std::terminate
()?在啓動或銷燬
thread
?
是否在標準中定義,我是否可以訪問thread_local
對象?在註釋中給出
在哪個線程被稱爲終止處理程序:在哪個線程中調用了終止處理程序?
當一個例外是noexcept
函數內拋出?
當用戶撥打std::terminate
()?
在啓動或銷燬thread
?
是否在標準中定義,我是否可以訪問thread_local
對象?在註釋中給出
這個答案總結答案,現在刪除了一個答案:
它不是標準中所規定(DeiDei,我已經在N4618檢查過)
然而,由於技術原因,該處理程序不太可能在其他線程中被調用,該線程導致呼叫std::terminate
(Galik,Hans Passant)
已經驗證的在線編譯器(里納特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,鏗鏘3.8與pthreads)
我的常識告訴我,它會調用線程調用'std :: terminate'?我確實相信標準沒有明確地解決這個問題。 – DeiDei
我相信這是不明確的行爲(或可能是undefined?) –
我會說在實際設置處理程序的線程,雖然idk –