Be careful to cancel a thread only when cancellation is safe. The pthreads standard specifies several cancellation points, including:
Programmatically, establish a thread cancellation point through a pthread_testcancel call.
Threads waiting for the occurrence of a particular condition in pthread_cond_wait or pthread_cond_timedwait(3C).
Threads blocked on sigwait(2).
Some standard library calls. In general, these calls include functions in which threads can block. See the cancellation(5) man page for a list.
Cancellation is enabled by default. At times, you might want an application to disable cancellation. Disabled cancellation has the result of deferring all cancellation requests until cancellation requests are enabled again.
See pthread_setcancelstate Syntax for information about disabling cancellation.