The following programs demonstrate how one thread can cancel (terminate) another using the library call pthread_cancel.
The first program, pthread_cancel.c uses pthread_cancel to cancel a thread at a function that libc designates as a cancellation point (here, either printf or sleep).
The second program, pthread_testcancel.c demonstrates a thread that calls pthread_testcancel to create a cancellation point in a function that otherwise would not have one.
The final program, pthread_cleanup.c demonstrates the use of pthread_cleanup_push and pthread_cleanup_pop to install a cleanup function that is called upon thread termination.
Additional files: