C Programming

C: pthread_mutex_lock Function Usage

As the name suggest the “pthread_mutex_lock” function must be used to lock something. The POSIX library of C came up with this function to lock a specific thread that may be used as a shared resource for some other function in a program. It is necessary to avoid deadlock while execution when two or more functions are using the same thread as their resource for execution completion. Therefore, we will be discussing the usage of the “pthread_mutex_lock” function of the C POSIX library in the Ubuntu 20.04 system.

Example 01:

Let’s start with the first example to see the mutex_lock() function of POSIX in C code. We have started with the file creation with Ubuntu’s “touch” instruction in its shell. This newly generated file can be found in your Linux home folder. To add the code in this file, you have to open it within some Ubuntu’s editor i.e., text, nano, or vim. We are using the Nano editor here for our code creation. Both the commands are listed in the image.

We are starting our C code with some C headers. These header packages include the use of standard input-output for code, standard libraries, string headers, and POSIX thread library. We have initialized a POSIX thread object “th” of size 3 i.e. it will create only 3 threads using IDs.

After this, the integer type variables are declared i.e., “I” and count”. The variable “I” is initialized to 0. Here comes the pthread_mutex_t variable to declare the “lock” for a thread. Although, the execution starts from the main() method we have to look at the Thread function first. This function is called as the Critical section of our code due to “mutex_lock” function. At the start of the Thread function, the pthread_mutex_lock function is utilizing the lock variable to lock the particular thread using its “ID” passed by the main() function pthread_create() method.

Now, no other thread can use this thread until this thread is unlocked. So, it will continue to process. The long type variable “I” is initialized to 0 for use in the “for” loop. The “count” variable has been incremented by 1. The count variable is used within the print statement to let us know that the “Thread1” is started now. For “loop” will be initialized here to give a moment of break to the execution of Thread. After that, the print statement will let us know that thread 1 is going to be finished.

The pthread_mutex_unlock() function is utilized as opposed to the pthread_mutex_lock() function to unlock the Thread number 1. Control goes to the main() method. The main() function continues to create the Thread function until the count reaches 3. Here comes the turn of the main() method after 3 threads creation, lock, unlock and exit.

The main() function is initialized with an integer variable “err”. The “if” statement is used here to check if the initialization of mutex thread “l” is failed using the “pthread_mutex_init()” function of POSIX’s. If the initialization fails, it will print out the particular message of the print statement. The “while” loop is here to see the condition i.e. “I” less than 3. It will confirm that the value of “I” is less than 3 and hence, continue to create a thread. Each thread will be locked when it is called and no other thread can be created until then.

If we got an error in the thread, we will display that error in the shell by converting it to string using the “strerror” method. The pthread_join() function is used to take back all the resources given to the threads. In last the “pthread_mutex_destroy()” function is used to destroy the lock object. Our program ends here.

The file has been compiled, and we have got no errors. On execution, The main() function started and created a thread 1.

After a while, due to lock, thread 1 completed its execution and finished. After that, the main() function created Thread 2 and it has been started.

After thread 2 is completely executed, the lock has been ended and the main() function created a last thread i.e., 3rd thread.

After the third thread is executed completely, the lock is released and the control is given back to the main method.

Example 02:

Let’s have another example to see the working of the “pthread_mutex_lock()” function of POSIX’s. The code has been started with the same header files.

After the header files, we have created a mutex lock function. There comes three functions. Two thread functions and 1 is the linked function. Thread1 and Thread2 are taking input from the main() function i.e. thread objects th1 and th2. Both thread functions are calling the show() method and passing two strings in its parameter. When the “show” function starts, it locks itself with the use of the “pthread_mutex_lock()” function utilizing the mutex lock object. The first print statement is taking the first argument and displays it. Then, it sleeps for 1 second, and the second argument value will be displayed via the print clause. In the last line, the lock has been released using the “pthread_mutex_unlock()” function utilizing the lock object.

The main() function is started with the creation of two objects for threads i.e. th1 and th2. Two threads have been created by the “pthread_create” function by passing th1 and th2 in the parameters. The “while” loop is used to just run and not finish for even a second. So, the program continues to process itself.

The code has been compiled first with the help of the “gcc” compiler in Ubuntu 20.04.

When the code got executed, show() method called using Thread1 and Thread2 function one after another. The program didn’t stop after the threads are executed. So, we have to stop the execution forcefully using the “Ctrl+Z” shortcut.

To prevent your system to do non-stop processing, we have to remove the “while” loop from the code in the main() method. The return 0 phrase has been replaced with the “while” loop.

Now, this program is ready to be compiled and executed. So, we have compiled this program with a “gcc” compiler. After that, the execution has taken place. You can see that the program is ended itself after the execution of two threads. The Thread1 worked and the show() function locked itself while execution. After the execution, it has released itself and Thread2 has been executed. The “show” function is called within it and passed some parameters. The “show()” function locked itself and doesn’t release until the execution has been done and the mutex_lock function is not called. After that, the control is given back to the main() method and the program ends.

Conclusion

This was all about what we can do to make you understand the usage of the pthread_mutex_lock function in C code. We had tried two extremely different programs to make it understandable for you and explained both the examples quite briefly and simply. We are quite optimistic that this article will be great for every C user.

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.

Page was generated in 0.075669050216675