We will first define a mutex object by the name lock. Recall that a mutex is basically a lock associated with a shared resource. To read or modify the shared resource, a thread needs to first acquire the lock for that resource. We will define two variables of the pthread_t type , with the names tid1 and tid2, to store two thread identifiers.
We will invoke the pthread_mutex_init method that initializes the lock object with the default mutex attributes. When it's initialized, the lock object is in an unlocked state. We then invoke the pthread_create function twice to create two threads and assign their identifiers to the two variables tid1 and tid2, whose addresses are passed to the pthread_create function. The two threads are created with the default attributes.
Next, we will execute the runThread function to create the two threads. In the runThread function, we will invoke the pthread_mutex_lock method and pass the mutex object lock to it to lock it. Now, the rest of the threads (if any) will be asked to wait until the mutex object lock is unlocked. We will invoke the pthread_self method and assign the ID of the calling thread to the variable id of the pthread_t type. We will then invoke the pthread_equal method to ensure that if the calling thread is the one with the identifier assigned to the tid1 variable, then the message First thread is running will display on the screen.
Next, the message Processing the common resource is displayed on the screen. We will invoke the sleep method to make the first thread sleep for 5 seconds. After a duration of 5 seconds, the message First thread is over will be displayed on the screen to indicate that the first thread is over. We will then invoke pthread_mutex_unlock and pass the mutex object lock to it to unlock it. Unlocking the mutex object is an indication to the other threads that the common resource can be used by other threads, too.
The runThread method will be invoked by the second thread, with the identifier tid2. Again, the mutex object lock is locked, and the id of the calling thread, that is, the second thread, is assigned to the variable id. The message Second thread is running is displayed on the screen, followed by the message Processing the common resource.
We will introduce a delay of 5 seconds to indicate that the second thread is processing the common resource. Then, the message second thread is over will be displayed on the screen. The mutex object lock is now unlocked. We will invoke the pthread_join method twice and pass the tid1 and tid2 thread identifiers to it. pthread_join is invoked to make the two threads and the main method wait until both of the threads have completed their tasks.
When both of the threads are over, we will invoke the pthread_mutex_destroy method to destroy the mutex object lock and free up the resources allocated to it.
Let's use GCC to compile the twothreadsmutex.c program, as follows:
D:\CBook>gcc twothreadsmutex.c -o twothreadsmutex
If you get no errors or warnings, that means the twothreadsmutex.c program has been compiled into an executable file, twothreadsmutex.exe. Let's run this executable file:
Voila! We've successfully used mutex to share data between two threads. Now, let's move on to the next recipe!