Let’s have the first example to see the working of the pthread_detach function. Start with the terminal by launching it, i.e., Ctrl+Alt+T. We will be using specific commands to create a C file, opening and executing it. The very first command is to generate a new file with it, i.e., touch. This new file needs to open in such an editor in which we can add code to it and make changes to it as well. We have been using the “nano” editor here via its command.
We have started the implementation of the first example with some header files that are needed for this code to run. Our code contains two user-defined functions and 1 main() method. As the execution always starts from the main() method, we also start the explanation from the main(). The main () function is called the “CreateT” method in its first line. Now the control is given to the “CreateT” function. This function creates an object “th” for getting the thread ID using pthread_t built-in mutable. The printf statement shows that we are currently in the main thread or 1st function.
The “pthread_create” function is used here to create a new thread in this function utilizing the name of another function, i.e., New and binding the “th” variable to reference the ID. The “if” statement is utilized to check whether this main() function and other newly created threads are equal or not. This has been done by equating the IDs of both threads. The “th” mutable reference to the New thread and pthread_self() returns the ID of the “CreateT” function. If both threads match, it will print “threads are same”; otherwise, “threads are not same.” The pthread_join() function ensures that the main thread execution gets terminated until the New thread is executed and not completed. Now the control is entirely over to New thread.
In New thread, the sleep function is used. So, the system will sleep for 10 seconds, and after that, further execution will take place. The pthread_detach() function is here to completely detach the New thread from its calling function, i.e., “CreateT.” Here, pthread_self() is used to find out the ID of ‘New thread’ for detaching. The printf statement will display that this function thread will exit now. The system will sleep for the next 10 seconds again using the same “sleep()” method of C. The pthread_exit() function is here to quickly terminate the current thread, which is right now “New.” Now, the control is given back to the “CreateT” function. After coming back in this main thread, we have encountered a new printf statement to display that we are back in the “CreateT” function. Now, we need to use another pthread_exit() function to close the “CreateT” thread as well and give the control back to the main() function. So, we have done it so far, and control is given back. Here, the program ends. As the program is finished, we need to compile it with the C compiler on Ubuntu 20.04.
You must ensure that the C compiler is already configured at your end machine. We have been utilizing the GCC compiler in our shell. So, the name of a file with the keyword “-lpthread” is used to compile the code as per the below image.
After compiling the code, we have to execute it to see the output. The command for execution is “./a.out” as below. When we have executed the code file, it started the main function and the main() function called the “CreateT” function. The printf statement of “CreateT” displayed “Inside Main Thread” and created a new thread named New. A comparison of both threads has been made, and it returns both the threads are not the same. Then, a system sleeps for 10 seconds.
After 10 seconds, it joins the created thread New. The New thread has been detached from the “CreateT” function and displayed that we are in the “New” thread function. The system sleeps again for the next 10 seconds and exits the New thread.
Now, the control is over to the “CreateT” thread, and it has sprinted out that we are back in the main thread. After the “CreateT” thread is completed, the main() function has been given control. Hence, the program ends here successfully.
Let’s take a quite different look at the pthread_detach function example in C. We have started our C code with the same library headers with #include keyword to make our code runnable. 1 main() function and 1 user-defined function named “New” is defined. The “New” function will be used as thread functions. We are starting the explanation from a main() method. The pthead_t mutable is declaring the “th” variable for getting the thread ID of a new thread. The printf statement shows that we have started the main function and goes for 10 second sleeps using the “sleep” method. The next printf displays that a thread function will be created and the pthread_create() function of POSIX is used for this reason so far.
The “th” is used as a parameter to the new thread creation function to get the ID of a new thread. The pthread_join() function is here to completely suspend the execution of a main() method until the new thread, i.e., New, is executing. Now, the New function is started. The pthread_detach() function is here to completely detach this function from the main() function by taking back its resources. The pthread_Exit() function will ensure that the new thread is not executed anymore. Therefore, its printf statement will not be executed. The main() thread will be terminated after executing its pthread_exit() function.
Let’s begin with the code compilation with gcc. Thankfully! It was successful.
So, we have been utilizing the same “./a.out” instruction here as well. The main() function started executing first as the print statement outputs. Now, the system sleeps for 10 seconds.
After 10 seconds, the next print statement is executed and displayed that the new thread is created. The control is over to New thread, and it’s detached from the main() function without executing its printf statement. Therefore, our output is something like below.
So, this was all about using POSIX’s pthread_detach function in C to completely detach a thread from the main calling thread. By keeping it quite simple and explaining the illustrations briefly, we have tried our best to make you understand these examples implemented in Ubuntu 20.04.