I want to know if there is an easy way to catch the id of the thread who crash without using intermediates signal_handlers
I have the following code
void signal_handler(int signal)
{
std::cout << "Caught the signal:" << signal << std::endl;
}
void handler_t1(int signal)
{
std::signal(SIGSEGV, signal_handler);
std::cout << "Thread 1 have received the signal: " << signal << std::endl;
std::raise(SIGSEGV);
}
void f1()
{
std::signal(SIGSEGV, handler_t1);
std::cout << "Begin thread 1" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
std::raise(SIGSEGV);
std::cout << "end thread1" << std::endl;
}
void f2()
{
std::cout << "Begin thread 2" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(3));
std::cout << "end thread2" << std::endl;
}
int main()
{
std::thread t1(f1);
std::thread t2(f2);
t1.join();
t2.join();
}
So I get the
Begin thread 1Begin thread 2
Thread 1 have received the signal: 11
Caught the signal:11
end thread1
end thread2
But if I have 5 threads, can I avoid to create 5 handler ?
edit my includes are
#include <thread>
#include <chrono>
#include <iostream>
#include <csignal>
edit I think I was looking something like I found https://stackoverflow.com/a/16259324/2380470 but since this thing doesn't exist yet, I must find something else