pthread_cancel(3) Library Functions Manual pthread_cancel(3) pthread_cancel - POSIX (libpthread -lpthread) #include int pthread_cancel(pthread_t thread); pthread_cancel() thread. : . pthread_setcancelstate(3) ( ) . . . pthread_setcanceltype(3) ( ). ( ). . pthreads(7). thread ( ): (1) ( ) . ( pthread_cleanup_push(3).) (2) . ( pthread_key_create(3).) (3) . ( pthread_exit(3).) pthread_cancel() pthread_cancel() . pthread_join(3) PTHREAD_CANCELED . ( .) pthread_cancel() 0 . ESRCH thread. attributes(7). +------------+------------------------------------+--------------------+ || | | +------------+------------------------------------+--------------------+ |pthread_cancel()| | MT-Safe | | | | | +------------+------------------------------------+--------------------+ . NPTL ( 32) . LinuxThreads SIGUSR2. POSIX.1-2008. glibc 2.0 POSIX.1-2001. . PTHREAD_CANCELED. : $ ./a.out thread_func(): started; cancelation disabled main(): sending cancelation request thread_func(): about to enable cancelation main(): thread was canceled #include #include #include #include #include static void * thread_func(void *ignored_argument) { int s; /* Disable cancelation for a while, so that we don't immediately react to a cancelation request. */ s = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); if (s != 0) errc(EXIT_FAILURE, s, "pthread_setcancelstate"); printf("%s(): started; cancelation disabled\n", __func__); sleep(5); printf("%s(): about to enable cancelation\n", __func__); s = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); if (s != 0) errc(EXIT_FAILURE, s, "pthread_setcancelstate"); /* sleep() is a cancelation point. */ sleep(1000); /* Should get canceled while we sleep */ /* Should never get here. */ printf("%s(): not canceled!\n", __func__); return NULL; } int main(void) { pthread_t thr; void *res; int s; /* Start a thread and then send it a cancelation request. */ s = pthread_create(&thr, NULL, &thread_func, NULL); if (s != 0) errc(EXIT_FAILURE, s, "pthread_create"); sleep(2); /* Give thread a chance to get started */ printf("%s(): sending cancelation request\n", __func__); s = pthread_cancel(thr); if (s != 0) errc(EXIT_FAILURE, s, "pthread_cancel"); /* Join with thread to see what its exit status was. */ s = pthread_join(thr, &res); if (s != 0) errc(EXIT_FAILURE, s, "pthread_join"); if (res == PTHREAD_CANCELED) printf("%s(): thread was canceled\n", __func__); else printf("%s(): thread wasn't canceled (shouldn't happen!)\n", __func__); exit(EXIT_SUCCESS); } pthread_cleanup_push(3), pthread_create(3), pthread_exit(3), pthread_join(3), pthread_key_create(3), pthread_setcancelstate(3), pthread_setcanceltype(3), pthread_testcancel(3), pthreads(7) 3 . . : . 6.18 21 2025 pthread_cancel(3)