Testing for Thread Cancellation

In Example 32-1, the thread created by main() accepted the cancellation request because it executed a function that was a cancellation point (sleep() is a cancellation point; printf() may be one). However, suppose a thread executes a loop that contains no cancellation points (e.g., a compute-bound loop). In this case, the thread would never honor the cancellation request.

The purpose of pthread_testcancel() is simply to be a cancellation point. If a cancellation is pending when this function is called, then the calling thread is terminated.

#include <pthread.h>

void pthread_testcancel(void);

A thread that is executing code that does not otherwise include cancellation points can periodically call pthread_testcancel() to ensure that it responds in a timely fashion to a cancellation request sent by another thread.