All threads share the same signal processing method
The main thread sets the processing mode of the SIGINT signal in order to capture, then all the secondary threads share this processing, and if one of the threads changes the way the signal is processed, then all the threads will share the change.
Recalling "signal shielding word" and "pending signal word"
Role
"Signal shielding word" and "pending signal word" function with process signal, reference ipc--signal
Modify the signal mask character function
int Sigemptyset (sigset_t *set);
int Sigfillset (sigset_t *set);
int Sigaddset (sigset_t *set, int signum);
int Sigdelset (sigset_t *set, int signum);
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);
When there is no secondary thread
At this time the entire process has only one main thread, the whole process of "signal shielding word" and "pending signal word", is the main thread "signal shielding word" and "pending signal word."
When a thread is available
Each sub-thread has its own independent "signal screen word" and "pending signal word", the second thread of "signal shielding word" and "pending signal word" is copied from the main thread "signal shielding word" and "pending signal word", at the time of copying,
(1) The "signal screen word" of the secondary thread retains the value copied from the main thread "signal mask word".
(2) The "pending signal word" of the secondary thread is emptied, meaning that the copied value is not preserved.
If the secondary thread has a special "response requirement" for some signals, it can call the following function and modify the secondary thread's own "signal mask word" to enable a signal to be opened or blocked.
int Sigemptyset (sigset_t *set);
int Sigfillset (sigset_t *set);
int Sigaddset (sigset_t *set, int signum);
int Sigdelset (sigset_t *set, int signum);
int sigprocmask (int how, const sigset_t *set, sigset_t *oldset);
How does multi-threading respond to a signal?
(1) After the process receives the signal, the process will find a thread that does not mask the signal to handle which thread is being searched for in this signal, and the process is the boss.
(2) If all the threads are blocking the signal, the process will record the pending signal to a thread's "pending signal word" when the signal occurs. Similarly, which thread to find, the process is the boss.
(3) When the thread receives the signal that the process has awarded it, and if the signal is processed or captured, the thread is interrupted at run time, then executes the signal capture function, and when the signal capture function is finished, it returns the interrupted thread and executes.
ipc--Thread Signal problem