Do not use signals in multithreaded programs. This is undefined behavior in C11 (Section 7.14.1.1, paragraph 7). Furthermore, in POSIX, sending an uncaught signal in order to kill a thread causes the signal to kill the entire process, not just the individual thread.
This rule is a specific instance of SIG02-C. Avoid using signals to implement normal functionality.
Noncompliant Code Example
This code raises a signal within a child thread. This is meant to terminate the program, but results in undefined behavior.
void func(void *data) { /* ... */ if (thread_should_exit) { raise( SIGTERM); // Undefined! } /* ... */ } int main(void) { int result; thrd_t thread; int result; if ((result = thrd_create(&tid, func, NULL)) != thrd_success) { /* Handle Error */ } return 0; }
Compliant Solution
This code terminates the child thread rather than raising a signal. This has the same effect as the noncompliant code example, but is well-defined in C11.
void func(void *data) { /* ... */ if (thread_should_exit) { thrd_exit(0); // OK } /* ... */ } int main(void) { int result; thrd_t thread; int result; if ((result = thrd_create(&tid, func, NULL)) != thrd_success) { /* Handle Error */ } return 0; }
Noncompliant Code Example (POSIX)
This code uses the pthread_kill()
function to send a SIGTERM
signal to the created thread. The thread receives the signal, and the entire process is terminated.
void func(void *foo) { /* Execution of thread */ } int main(void) { int result; pthread_t thread; if ((result = pthread_create(&thread, NULL, func, 0)) != 0) { /* Handle Error */ } if ((result = pthread_kill(thread, SIGTERM)) != 0) { /* Handle Error */ } /* This point is not reached because the process terminates in pthread_kill() */ return 0; }
Compliant Solution (POSIX)
This compliant code uses instead the pthread_cancel()
function to terminate the thread. The thread continues to run until it reaches a cancellation point. See The Open Group Base Specifications Issue 6, IEEE Std 1003.1, 2004 Edition [Open Group 2004] for lists of functions that are required and allowed to be cancellation points. If the cancellation type is set to asynchronous, the thread is terminated immediately. However, POSIX requires only the pthread_cancel()
, pthread_setcancelstate()
, and pthread_setcanceltype()
functions to be async-cancel safe. An application that calls other POSIX functions with asynchronous cancellation enabled is nonconforming. Consequently, we recommend disallowing asynchronous cancellation, as explained by POS47-C. Do not use threads that can be canceled asynchronously.
void func(void *foo) { /* Execution of thread */ } int main(void) { int result; pthread_t thread; if ((result = pthread_create(&thread, NULL, func, 0)) != 0) { /* Handle Error */ } if ((result = pthread_cancel(thread)) != 0) { /* Handle Error */ } /* Continue executing */ return 0; }
Risk Assessment
Mixing signals and threads causes undefined behavior.
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
CON37-C | low | probable | low | P6 | L2 |
Bibliography
[OpenBSD] signal()
Man Page
[ISO/IEC 9899:2011]
[MKS] pthread_cancel()
Man Page
[Open Group 2004] Threads Overview