/*0. Cancel thread int pthread_cancel (pthread_t thread), set cancel point void Pthread_testcancel (void), test whether receive cancellation request, if any, end thread. Example:*/#include<stdlib.h>#include<pthread.h>#include<stdio.h>#include<sched.h>intA =0;void*thread1 (void*Arg) {Pthread_testcancel (); This function is very important to be used in conjunction with pthread_cancel (T1); A=Ten; } /*if instead: int a = 0;void *thread1 (void *arg) {a = 10; Pthread_testcancel (); Run Result: A value was modified by main Startmain end, a=10*/intMainintargcChar*argv[]) {pthread_t T1, T2, T3; intret, I; printf ("Main start\n"); RET= Pthread_create (&t1, NULL, THREAD1, NULL); Pthread_cancel (t1); Pthread_join (t1, NULL); Sleep (3); printf ("main end, a=%d\n", a); return 0;}/*Run Result: At the end of the program at the cancel point, the a value is not. Main Startmain end, a=0*/
. Canceling a thread