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.
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.
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.
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.
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”.
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:
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:
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.
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.