Use pthread_cancel(3THR) to cancel a thread.
Prototype: int pthread_cancel(pthread_t thread);
#include <pthread.h> pthread_t thread; int ret; ret = pthread_cancel(thread);
How the cancellation request is treated depends on the state of the target thread. Two functions, pthread_setcancelstate(3T) and pthread_setcanceltype(3T), determine that state.
pthread_cancel() returns zero after completing successfully. Any other returned value indicates that an error occurred. When the following condition occurs, the function fails and returns the corresponding value.