pthread_cancel() in C with example

06 Sep, 2017
prerequisite: Multithreading, pthread_self() in C with Example
pthread_cancel() = This function cancel a particular thread using thread id. This function send a cancellation request to the thread.
Syntax : – int pthread_cancel(pthread_t thread);
First Program : – Cancel self thread
 
// C program to demonstrates cancellation of self thread // using thread id#include <stdio.h>#include <stdlib.h>#include <pthread.h>  void* calls(void* ptr){    printf("GeeksForGeeks");      // To exit the current thread    // pthread_self() return the particular thread id    pthread_cancel(pthread_self());           return NULL;}  int main(){    // NULL when no attribute    pthread_t thread;      // calls is a function name    pthread_create(&thread, NULL, calls, NULL);       // Waiting for when thread is completed    pthread_join(thread, NULL);       return 0;}
Output:
GeeksForGeeks

 
If you use Linux then compile this program gcc program_name.c -lpthread
Second Program : – Cancel other thread
 
// C program to demonstrates cancellation of another thread // using thread id#include <stdio.h>#include <unistd.h>#include <sys/types.h>#include <pthread.h>  // To Countint counter = 0;   // for temporary thread which will be // store thread id of second threadpthread_t tmp_thread;   // thread_one call funcvoid* func(void* p) {    while (1) {          printf("thread number one\n");        sleep(1);   // sleep 1 second        counter++;                   // for exiting if counter = = 5        if (counter = = 2) {              // for cancel thread_two            pthread_cancel(tmp_thread);               // for exit from thread_one             pthread_exit(NULL);          }    }}  // thread_two call func2void* func2(void* p) {      // store thread_two id to tmp_thread    tmp_thread = pthread_self();       while (1) {        printf("thread Number two");        sleep(1); // sleep 1 second    }}  // Driver codeint main(){      // declare two thread    pthread_t thread_one, thread_two;       // create thread_one    pthread_create(&thread_one, NULL, func, NULL);      // create thread_two     pthread_create(&thread_two, NULL, func2, NULL);       // waiting for when thread_one is completed    pthread_join(thread_one, NULL);       // waiting for when thread_two is completed    pthread_join(thread_two, NULL);   }
Output:
thread number one
thread number two
thread number one 
thread number two

 
If you use Linux then compile this program gcc program_name.c -lpthread
This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
C-Library
C Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Substring in C++
Function Pointer in C
Multidimensional Arrays in C / C++
Left Shift and Right Shift Operators in C/C++
Different Methods to Reverse a String in C++
std::string class in C++
Unordered Sets in C++ Standard Template Library
rand() and srand() in C/C++
Enumeration (or enum) in C
What is the purpose of a function prototype?