Sending a uncaught signal to a thread to terminate it should be avoided, as it kills the entire process as opposed to killing 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 instead uses the pthread_cancel()
to terminate the thread. In the thread function we set the cancel type to asynchronous, so as to ensure an immediate cancel. If desired, the cancel type can be left as the default type of deferred, where the thread will not terminate until it reaches a cancellation pointThe thread will then continue to run until it reaches a cancellation point. See the second referenced article for a list of functions that are cancellation points. If the cancellation type is set to asynchronous, the thread will be terminated immediately, however in most cases it is not safe to do so, and should be generally avoided.
Code Block | ||
---|---|---|
| ||
int main(int argc, char* argv[]){ pthread_t thread; pthread_create(&thread, NULL, func, (void*)0); sleep(1); pthread_cancel(thread); /* Continues */ return 0; } void func(void *foo){ pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); /* Execution of thread */ } |
Risk Assessment
Using signals as described has the simple consequence of terminating the process, which is clearly undesired. However there is no other direct risk.
...
Wiki Markup |
---|
\[[OpenBSD|AA. References#OpenBSD]\] [{{signal()}} Man Page|http://www.openbsd.org/cgi-bin/man.cgi?query=signal]
[http://www.mkssoftware.com/docs/man3/pthread_cancel.3.asp] |