C Programming

C Pthread_self Function Usage

C language comes up with a lot of libraries used for specific purposes. One of those libraries is the “POSIX” library of C that is specifically for POSIX systems. The POSIX library can let you make threads, get their IDs, and do many things. One of those functions is the “pthread_self()” function that is specially designed to get the ID of the created new thread. So, we will be discussing some of the examples to use the “pthread_self()” function in C. Let’s get started.

We have started implementing this article to create a newly generated C file. This file can be created using Ubuntu’s “touch” instruction within its shell. It’s quite easy to do so as we did it in the command shown in the attached screenshot. There are several built-in editors available in Ubuntu to simply open and edit the C code. We have utilized the built-in “nano” editor in a terminal of Ubuntu 20.04 using the “nano” keyword in the instruction.

Example 01:

So, we are starting our first example of using the pthread_self() function in the C code to get the “ID” of a thread in Ubuntu 20.04. We have initialized the code with simple headers. These headers are the necessities of the C code. Without these headers, our code is of no use, and we will be unable to get the required result. We have included the “stdio” header to take the standard input-output in our code. The standard library header file is also used. Meanwhile, the main header file is “pthread,” which has been included to use the standard POSIX thread library and its functions in the C code.

After the libraries are included, we have used the user-defined thread function named “Task” taking pointer reference to the “ID” of a thread created in the main function. Within this Task function, we utilize the printf clause to output a statement showing that we are executing the thread having a particular “ID.” The thread “ID” that has been got from the use of the “pthread_Self” function in this statement will be displayed along with the text in the shell via the “printf” clause.

The “pthread_exit” function is utilized here to exit the created thread and return NULL to the main function, as shown by the “return” clause of the “Task” function. Here comes the main execution code of a file. The execution of C code will always be initiated from its main() function. The main() function is using the POSIX’s “pthread_t” variable to get the ID of a thread created by the “pthread_create” function within the “th” mutable. The simple “printf” clause is cast-off here to output on the terminal that the main function has been executing or running right at the moment. After this, the “pthread_create” function is here to create a new thread. The “th” variable has been referenced here to identify this thread by its ID and getting NULL parameters. The “Task” function has been called here to use it as a thread executer or show information regarding the thread ID. The Task function is taking no arguments as per the NULL. After the “Task” function is executed and returned “NULL,” the pthread_join() function is used here to stop or hang the calling thread while waiting for the directed thread to complete or terminate. The C code for pthread_self() is completed and compiled in the shell.

Let’s use the “GCC” compiler that is already been installed in our Ubuntu 20.04 system. Your code will not output the required thing on the shell without it. So, you must get the “GCC” compiler. The compilation for pthread_self() is tremendous, and we are ready to run the code. The run code is used as a single keyword “./a.out” as below. You can see that the main function was executed first and created a thread. Through this thread, the “Task” function is called, and it displays the thread ID in the shell. The Task function was completed, and the main() thread was terminated. This is how the “pthread_self” function works.

Example 02:

Let’s take another illustration to have a look at the “pthread_self()” function in C code. For that, we will be updating the same “self.c” file just used in the above example. We will be opening the file within Ubuntu’s editor named “Nano” as shown beneath.

The code is started again with some headers. The standard input output header is utilized to get the input and display it. The “unistd” and “stdlib” libraries are used to utilize some standard functions within the code. The “pthread” library is a must to utilize here for using the “POSIX” functions in the code. We have defined the number of threads using the “THREADS” of POSIX in the code after the libraries using the “if” statement and the use of the “#define” keyword. It is used to restrict the number of threads used in the code to not more than 3. After the THREADS declaration, we have used the user-defined function “Display” of pointer type has been declared. This function takes the argument “p” of pointer type. The long type variable “id” is declared.

At the next line, the reference pointer variable value is converted to “long” type and has been assigned to the variable “id.” The printf clause uses the Thread number and ID to display on the shell using “ld” and “lu” in the text. After this, the thread has been exited. Now, the main() function is declared with two arguments. The “pthread_t” is used to initialize the list “th” having a size of 3 threads using “THREADS” already declared. Then an integer variable “res” is declared, and the “for” loop has been started. It will run up to 3 times, and three threads will be created using the “pthread_create” function. This function utilizes the IDs of three threads consecutively, one after another, and the Display function uses those IDs as their argument to pointer “p.” If any error occurred due to the “Display” method’s, the thread not being completed successfully. It will return the error to the main functions “res” variable. If an error occurs, the “printf” statement will display that error using the “res” variable in it within the “if” statement. After that, the if statement and program will be terminated.

Now, it’s high time to save and compile this C code in the system. For saving, use “Ctrl+S” while staying in the editor. Use “Ctrl+X” to exit the C code editor. After the “gcc” compilation command, usage in the shell succeeded. We ran this code with the “./a.out” instruction, and the output shows the IDs of three different threads consecutively.

Conclusion:

This article has been covered the simple and most straightforward examples of using the pthread_join() function of the POSIX library in C. Firstly, we have explained the usage of POSIX’s “pthread_join()” function in the C language. After that, we have discussed some of the examples to illustrate its working in the C program. We have faith that all the examples are remarkable and easily be implemented on the Linux editor and run on the shell.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.