This answer sum up answers given in comments and an answer now deleted:
It is not specified in the standard (DeiDei, I have checked too in N4618)
Nevertheless, for technical reasons it is unlikely that the handler is called in an other thread that the one who caused the call to std::terminate
(Galik,Hans Passant)
it has been verified on online compiler (Rinat Veliakhmedov), the terminate handler is called in the thread that causes terminate to be called.
You can test it yourself with this code from a deleted answer:
#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();
}
Conclusion It is unspecified but it seems that the handler is always be called in the thread that causes std::terminate
to be called. (tested on gcc-5.4, gcc-7.1, clang-3.8 with pthreads)