You have to create the same code as shown in the image attached below. This code has been initiated with the addition of header files for the C language. Without these headers, our code will be of no use. Then standard output and input will be used in the system, and a terminal with these headers and the POSIX thread library is required to make use of its thread functions. After these header files, we explain this code from its main() function. This is because the execution of the program starts from here.
So the “pthred_t” is used here to initialize a thread object “th”. The print statement shows us that we are currently starting from the main() function and creating a Thread from it. Then, the execution of this code halts for 10 seconds, and the code uses the “sleep” function to sleep for a while. The main thread creation function is named “pthread_create” using the thread object “th” and the function name, i.e., Thread. Now, the Thread function has started executing itself. The new Thread function of pointer type takes an argument of pointer type. This function makes simple use of the “sleep” method to make your system and execution sleep or halt for 10 seconds.
The printf statement of this thread lets us know that we are currently in the Thread function, i.e., not in the main() function. Here comes the “pthread_cancel” function that has been using the “pthread_self()” function to get the ID of the thread to cancel the execution of the current thread, i.e., Thread. As the thread is canceled, the control is over to the main() function. Within the main method, the pthread_join function of POSIX is utilized here to make sure that the Thread function is terminated and takes back all the resources from it. The sleep function will make our system again sleeps for 10 seconds. The printf statement of main will display that we are back in the main() method and now the program is ending. Our program is now complete and ready to be used in the commands for execution and compilation purposes.
The “gcc” compiler is the best while working in the Ubuntu terminal. So, we have been using it here to the compiler of cancel.c file of POSIX.
On execution, the main() method starts and creates a thread, i.e., Thread. Our system sleeps for 10 seconds.
The control is given to the Thread function, and it displays that we are currently in the thread function while executing. After that, the thread has been canceled, and resources are taken back using the “pthread_join” function in main(). The system again sleeps for 10 seconds.
The print statement shows that we are in the main and the program is ending.
Let’s have another new example to see the usage of the pthread_cancel function in C programming. So, the program has been started with the addition of header files, i.e., standard input-output, Unicode standard, system-types header, and POSIX thread library. A global variable “count” of integer type is initialized to 0 at the top of this program. A pthread_t object named “thr” has been declared as global, which will be used as a temporary thread.
When the main() function creates the first thread, i.e., th1, it will call the Thread1 function and pointer argument. You have to ignore the printf statement to print the count value as it is of no use. The “while” statement is used to ensure that the Thread1 is called and executes itself until the loop breaks. So, the thread will sleep for 10 seconds and print out that we are working in Thread1. The “count” variable is incremented, and it is now 1. The “if” statement will be executed when the count value is 2. So, the control goes to Thread2.
The temporary thread object is saving ID of Thread2 using pthread_self. It also prints that we are in Thread2 and sleep for 10 seconds. Then, the control goes to Thread1, sleeps for 10 seconds, prints out the message, and increments the count, i.e., now 2. The Thread2 will be called and executed. The “if” statement will be executed, and Thread2, i.e., will be canceled using the temporary object “thr.” The pthread_exit function stops the Thread1 as well.
Here is the main() function from which the execution will start. Two local threads have been declared with the POSIX “pthread_t” keyword, i.e., th1 and th2. The print statement shows us that we are creating 1st thread, and the sleep function will make our system sleep for 10 seconds. The “pthread_create” function takes the object to a thread 1, i.e., th1 to create a thread “Tread1”. Now the Thread1 function is called. The next print statement shows that the second thread has been creating and the system will go to sleep for the next 10 seconds.
The “pthread_create” function is again here for the same purpose, i.e., creation of Thread2 i..e using th2. After the execution of both threads, the “pthread_join” function will make sure that the called thread is completed and terminated so that it can take back all the resources assigned to that thread. Here two pthread_join() functions are used for Thread1 and Thread2 separately. The program utilizes the sleep method once again to sleep for the next 10 seconds, and the print statement tells us that we have back in main and the program is ending here.
After compiling and running this code, The main() function started executing as shown. It is going to create Thread1 after 10 seconds.
Thread2 will be created by the main() function after 10 seconds of sleep.
Both the threads are running as the count is 0.
Threads are running again as the count is 1.
After the count approaches value 2, it will cancel theThread2 first.
Goes to Thread2 and canceled it. The control went to Thread1 and ended it. After that, main() function ends.
This guide was all about the POSIX pthread_cancel() function to cancel a thread in a program. For this, we have also utilized the pthread_create function to create a thread, the pthread_join() function to make sure a thread is terminated, the pthread_exit function to come out of the thread, and the pthread_self() function to get an ID of a thread. We hope this will be quite useful for every C user.