C Programming

Pthread_exit 3 C Function

“You may have gone through many POSIX C functions while using any Linux operating system. The pthread_Exit() function is one of those POSIX functions. The pthread_exit() is utilized to terminate the execution of the calling thread. The calling thread is terminated using the pthread_exit() function, and its execution is suspended using the pthread_join() function until the target thread has completed its execution. So, it terminates the calling thread and sends status to any thread that uses the thread ID of the termination thread to call pthread_join(). Within this tutorial, we will take a look at the usage of the pthread_kill function in C.”

Firstly, you need to log in from the Linux system and launch its terminal. Within the shell, try creating a C file using the VIM editor and name it pthread_Kill.c. When it is opened in the VIM editor in insert mode, write the below lines of code in it.

The essential header files are incorporated in the first three lines of code, and then the function header for the display message function, which accepts a void pointer, is declared. Create a global variable after that, and set its value to 0. Two variables of type pthread_t named thread_P & thread_S are declared. Pthread_t is a data type used to identify a thread that has been created in the main function. When the program makes function calls that call for a thread identification, it takes the value returned by the pthread_create() function.

Two character type pointer variables that are used to store the message for the primary and secondary threads are defined in the following lines of code. Two integers are then defined, with values of 100 and 200, respectively. Following that, we made two separate threads, each of which uses the display message method. Send the individually generated thread identifiers and messages to each thread separately; after that print, the statement is called, which is the message before the termination of the threads. When we exit or terminate the threads then, all the execution is suspended, and no output will be displayed. The global variable is incremented by 1 in the display message function.

See the available files and folders in the working directory by entering the ls command in the command line of the terminal.

Enter the commands listed below to build a file that contains thread-related statements because they differ from standard compilation instructions. The command is initiated with the compiler because we are using the GCC compiler and the Linux operating system. The -pthread switch is then employed. After specifying the file name with the -o flag, the output file will be created with the requested name.

Simply enter the command (./pthread Kill.out) in the terminal to run the output file and view the results. No statement will be executed after the before statement since both threads are ended using the pthread_exit function. As soon as the primary thread has been established, it calls the display message method, incrementing the global variable’s value from 0 to 1, which is then displayed. Similar to that, a second thread is then invoked, which displays the value of the global variable after an increment, resulting in a value of 2.

Now create another C programming code file using vim editor and write the code into it. The file name is pthread_Exit.c.

The vital header files are incorporated in the first two lines of the code file. From that, a global variable was declared, and its value was set to 0. The display function is then written, and a void pointer type parameter is given to it. All of the threads that we create in our main function will execute this code. The thread id variable’s argument value is stored in the function’s first line. Now that a static variable has been defined and set to 0, its value will be modified within the function. Then, we printed the thread id and the values of the global and static variables after incrementing them.

Use the GCC compiler to compile the c code with the -pthread option, then save the output to the pthread Exit.out file. Only one thread can complete its execution since we use the same thread ID to generate several threads and then exit the thread inside the loop.

Here is the output of the file after successful compilation. It means that if any thread is terminated using an exit statement, no other thread will be executed.

Each thread is generated and executed correctly if we slightly modify the code by changing the exit statement and moving the line outside the loop. This is the piece of code given in the snippet below:

After compilation and when we execute the output file, the below output will be generated, which shows that all threads completed their execution. The output shows that the values of global and static variables are the same for all threads, which means that the same thread reference is used.

Let’s change our code a little bit more and comment out the pthread_exit statement at the end of the display function. Here is the screen of the code:

Here is the output screenshot of the above code. Only just three threads are executed; you can see the values of the global and static variables. The second and third threads have the same values for both.

Conclusion

This article is all about the use of the pthread_kill function in C to terminate the processing of a calling thread in a program, but the termination process of the calling function must be halted for a while utilizing the pthread_join() method until the target method is accomplished. You can make use of the simple examples demonstrated above to get a clear understanding.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.