C Programming

Pthread_join Multiple Threads By Example

A thread is a sub-process that processes a certain part of code and owns its buffer. In this tutorial, we will be discussing “pthread_join” and some of its examples. A threaded set is a collection of threads that are running in a very similar operation. Inside a method, a thread is a solitary series flow. Threads are often referred to as frivolous processes since they share several of the characteristics of processes. Threads, unlike processes, really aren’t autonomous of one another, so they associate their script, information, and OS services such as open documents and triggers with the further threads. The execution of a pthread is obtainable through the gcc compiler. Before going further, you must understand two POSIX multithreading concepts that we will be using in today’s topic.

Pthread_create:

Whenever a multi-threaded code begins to run, it only has a single process operating, which performs the program’s main() operation. This thread does have its process ID and is now a filled thread. The pthread_create() method must be used to construct a fresh thread in the script.

Pthread_join:

For threads, the pthread_join() method is identical to wait for functions. The calling thread is blocked before the thread with the specifier equivalent to the first statement finishes.

Install GCC Compiler:

When working on a Linux system, you must have some compiler mounted on your system to compile your C code. The most recommended one is the GCC compiler. Therefore, log in from the Linux system and open the console terminal using “Ctrl+Alt+T”. You may also open it from the search bar of the Activity area. Now the terminal is opened, execute the below installation command for the “gcc” compiler to install it. Add your account password upon request and hit the “Enter” key. Now the gcc compiler has been installed; we will try some examples to elaborate on the “pthread_join” concept.

$ sudo apt install gcc

Example 01:

We have to create a new file, “one,” in a GNU Nano editor with the “c” extension. This is because we will be working on the C language. Try out the beneath instruction.

$ nano one.c

Type the below-displayed script in the nano file. The code is consists of some libraries to be used for POSIX multithreading, especially the “pthread.h”. We have created a method, “Thread”. The thread sleeps for 1 second and prints a statement. After that, the main function has been created. The variable “id” has been used as the “pthread_t” type to recognize the thread. Then a print statement will be executed, and a POSIX thread has been created using the “pthread_create” function. This function has 4 argument values. One of them is the pointer variable “id,” and the third one is the function “Thread” to be executed. All the others are default. Another print statement has been used, and the main method ends.

Save the nano file and quit using “Ctrl+S” and “Ctrl+X,” respectively. Let’s compile the code using the “gcc” compiler. But make sure this time you have to use a flag “-lpthread” in the command. Otherwise, code won’t be compiled and executed. Execute the following query.

$ gcc one.c –lpthread

Now run the script via the “a.out” instruction as below. Whenever the code has been executed, the main function operates first. So, the print statement has been executed, and the terminal displayed “Before Thread”. Then the function “pthread_create” has been executed, and it has created a new thread that uses the function “Thread”.After that, the “pthread_join” method has been used to move the control to the function “Thread”. In the “Thread” method, the program sleeps for 1 second and then executed the print statement, due to which the terminal displays “Within Thread”. After the “Thread” function has been executed, the control has again moved to the main function. And the print statement in the main function has been executed as “After Thread”.

$ ./a.out

Example 01:

Let’s take another example of the “pthread_join” function. This time we will not use default values as arguments to a thread. We will assign proper values to the thread. Create another file “two.c” in a nano editor to be used for the C language script as follows:

$ nano two.c

Write out the below-shown C code in the editor. We have defined a “Thread” function without any implementation. The main function started with some integer-type variables “i1” and “i2” specified. These two integer-type variables will be used as descriptors. Two “pthread” type identifiers, “t1” and “t2,” and other character type variables, have been used. Two “pthread_create” functions are specified to create two threads separately while using the thread “ID” and “messages” as their parameters. The “Thread” function is specified as a thread function where the parameters have been passed. The “Thread” method will take arguments and print the message. Then two “pthread_join” methods are used to restrict the current function. Two print statements will show some messages, and the main function quits.

Compile the file “two.c” with “gcc, along with the “-lpthread” flag as follows:

$ gcc two.c -lpthread

Let’s execute the code through the beneath command in the console. The output displays the result of the first two print statements of the main function as “Thread 1” and “Thread 2”. Then, due to the creation of threads, the control goes to the “Thread” function. After executing the “Thread” method, it moves back to the main function, and the other two print statements have been executed.

$ ./a.out

Conclusion:

Apart from a real machine, the thread ordinarily shares its storage with several other threads (although for tasks, we typically have poles apart storage zone for every one of them). They all have references to the very identical global variables, heap space, document descriptors, and so on since they share storage.

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.