C Programming

C pthread_create Function Usage

In Computer language, a thread is said to be a part of a Process or task. It is called multi-threading when we divide a single task or process into parts to make our multiprocessor achieve maximum efficiency and quality. The C language comes up with the concept of threads and utilizes the POSIX thread library to do multi-threading, create multiple threads, and use them simultaneously. One of these threads is “pthread_create” which creates a new thread or function to perform some specific task. Thus, we will see how a pthread_create function works in Ubuntu 20.04. Let’s take a look at it now.

We have started this article with the installation of a C compiler named GCC used within the Linux platform. Let’s start doing so by opening the Ubuntu 20.04 shell application. This could be happened by the use of the shortcut key “Ctrl+Alt+T.” The shell has been opened on your screen. We are now ready to install GCC. For this, we have utilized Ubuntu’s apt package in the installation command, as seen in the output. It asks for the sudo account password, and we have to enter it to complete the installation. It is completely installed now.

After the GCC compiler installation, we will make our system up to date so that all the updates are fully applied to the system. So, the use of an apt package is required here to update and upgrade. The upgrade and update are started now.

Within the upgrade, it may ask for the continuation of an installation process. You have to press “y” and continue using Enter key. The process will be completed in some seconds.

Example 01:

Let’s start with the basic one. Make a whole new C file with the simple “touch” keyword in the shell. It will generate a file in the home folder of the Linux system. You can try opening it using any built-in installed editor. Linux system came up with the Vim editor, Nano editor, and Text editor. We have preferred it to open within the GNU Nano editor. The query is shown below.

The create.c file is now already empty in the Ubuntu editor. To start the C code, we have added the library files for it. These libraries are necessary for the C code execution and make it error-free. These libraries will help us to use input and output statements, i.e., variables and print statements. The “pthread.h” header is used to use POSIX thread functions in C. The user-defined pointer function “Task” has been declared as having a pointer type argument. It utilizes a single printf clause to display that this function is a thread function utilizing the “pthread_self()” function to get its ID. After that, the thread was exited. The main function is utilized to start the execution of this program.

The pthread keyword is used to create the variable “th” to get the ID of the thread. The “printf” statement is here to tell us that we are executing a main thread function. Here comes the most important thing and the topic of this article, i.e., pthread_create. We have created a new thread using the POSIX pthread_create function. This function takes the reference to thread id “th” by using it and calling a “Task” function an argument. The pthread_join() function is called here to hang up the carrying out of a calling function after the completion or execution of the actual thread function. If the actual thread function is not completed in any situation, it will throw an exception that the thread is not completely executed.

Let’s come up with the simple compilation “creat.c” file in the terminal. For this purpose, we have to make sure that we use the “gcc” compiler along with the file name and the flag for POSIX, i.e., “-lpthread.” The compilation is prosperous as it doesn’t return any error. After this, we need to use the “./a.out” command to simply run this compiled “create.c” file. It would be returning the output as shown below. Firstly, it has taken the thread ID and displayed that the main function is running. Then a thread has been created via the “pthread_create” function, which utilizes the Task function in its parameter to be a new thread that will be created. The second NULL attribute shows that the new thread “Task” will utilize the default attributes. The 4th NULL value shows that the NULL value has been passed to the “Task” thread. The thread got executed, and the pthread_join() executes after that to end the main function.

Example 02:

Let’s take another illustration of the POSIX pthread_create function in C. We have initialized some of the well-known and most used libraries. The same Task function is declared using the printf statement. The main function is creating a POSIX thread variable “th” to store the ID of a new thread to be created. After printf statements, the system will sleep for 10 seconds, and the thread will be created in the same way used in the above illustration. After the execution of the thread, the system will again sleep for 10 seconds, and the program ends after the printf clause.

The compilation and running of this updated script are quite smooth as below. The main function started, and it displayed that the thread had been created. It sleeps for 10 seconds here.

After the thread, the control goes to the newly generated thread to execute it. After the completion, the system again sleeps for 10 seconds.

After 10 seconds, it deallocates all the resources used viapthread_join() and displays that we are back in the main function.

Example 03:

Here comes the last example. We have declared the C libraries, especially the “pthread” header. An integer type variable “v” is initialized with a value of 5. The Task function is declared utilizing the pointer type reference value, i.e., “ID.” It will print the thread value that has been passed to it by reference via the “printf” clause. Then, It may sleep for a while and exit this thread by passing reference to the variable “v” value, i.e., return 5. The main() method is creating POSIX thread object ID variable “th” and initialized variable “x” with 1. After the printf clause and sleep() function, it would create a thread. Here it is passing the value of variable “x” as a reference to the thread “Task” to be created with reference to the ID “th.” A pointer “p” is declared and used in the “pthread_join()” function to get the returned reference value “v” from the thread Task. In the next line, it will display that value.

While execution, it sleeps for 10 seconds when the main() method starts.

A thread is created and executed showing the value 1 passed by the main() function and sleeps again.

The pthread_exit references the value 5 of “v” to the main() function while the execution of thread “Task” is completed.


This article explains the use of the “pthread_create” function in C. It also discusses how to exit the thread and pass the value to the thread function. It simply covers all the knowledge about the pthread_create function of the POSIX library of C in Ubuntu 20.04. We hope it will be helpful for everyone looking for something brief and straightforward.

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.