In a C++ multi-thread program, if multiple threads crash at the same time, the SIGABRT signal is triggered, but I want to do only once signal processing to print the error message and then exit, how should I deal with it?
I tried to use sigaction()
, but after testing I found that the signal processing function still entered at the same time in different threads, the test code is below:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string>
#include <atomic>
#include <sys/syscall.h>
#define gettid() syscall(__NR_gettid)
#define THREAD_NUM 10
int Signal(int signo,void (*func)(int))
{
struct sigaction act;
act.sa_handler = func;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, SIGABRT);
sigaddset(&act.sa_mask, SIGBUS);
sigaddset(&act.sa_mask, SIGSEGV);
sigaddset(&act.sa_mask, SIGILL);
sigaddset(&act.sa_mask, SIGFPE);
act.sa_flags = 0;
return sigaction(signo,&act,NULL);
}
void SigHandler(int Signal){
printf("tid: %ld, access signal: %d\n", gettid(), Signal);
sleep(3);
_exit(1);
}
void* worker(void* args){
for(;;){
int a = std::stoi("");
}
}
int main(){
Signal(SIGBUS, SigHandler); // 10 Core Bus error (bad memory access)
Signal(SIGSEGV, SigHandler); // 11 Core Invalid memory reference
Signal(SIGABRT, SigHandler); // 6 Core Abort signal from abort(3)
Signal(SIGILL, SigHandler); // 4 Core Illegal Instruction
Signal(SIGFPE, SigHandler); // 8 Core Floating point exception
// multi thread
pthread_t pids[10];
for(int i = 0; i < THREAD_NUM; i++){
int err = pthread_create(&pids[i], NULL, worker, NULL);
if(err != 0){
perror("pthread_create err");
return -1;
}
}
for(int i = 0; i < THREAD_NUM; i++){
pthread_join(pids[i], NULL);
}
return 0;
}
I compile the code by g++ test_sto.cpp -g -Wall -std=c++11 -lpthread -o test_sto
.
the output is below:
terminate called after throwing an instance of 'std::invalid_argument'
what(): stoi
tid: 1652380, access signal: 6
terminate called recursively
tid: 1652382, access signal: 6
terminate called recursively
tid: 1652384, access signal: 6
terminate called recursively
tid: 1652386, access signal: 6
terminate called recursively
tid: 1652378, access signal: 6
terminate called recursively
tid: 1652381, access signal: 6
terminate called recursively
tid: 1652383, access signal: 6
terminate called recursively
tid: 1652385, access signal: 6
terminate called recursively
tid: 1652379, access signal: 6
terminate called recursively
tid: 1652377, access signal: 6
In a multi-thread program, how do I mask the specified signal in signal processing?