- Define two variables of the pthread_t type to store two thread identifiers. Also, define a mutex object:
pthread_t tid1,tid2;
pthread_mutex_t lock;
- Invoke the pthread_mutex_init method to initialize the mutex object with the default mutex attributes:
pthread_mutex_init(&lock, NULL)
- Invoke the pthread_create function twice to create two threads, and assign the identifiers that we created in step 1. Execute a function for creating the two threads:
pthread_create(&tid1, NULL, &runThread, NULL);
pthread_create(&tid2, NULL, &runThread, NULL);
- In the function, the pthread_mutex_lock method is invoked and the mutex object is passed to it to lock it:
pthread_mutex_lock(&lock);
- Invoke the pthread_self method and assign the ID of the calling thread to a variable of the pthread_t type. Invoke the pthread_equal method and compare it with the variable to find out which thread is currently executing. If the first thread is being executed, display the message First thread is running on the screen:
pthread_t id = pthread_self();
if(pthread_equal(id,tid1))
printf("First thread is running\n");
- To indicate that the thread is executing a common resource, display the text message Processing the common resource on the screen:
printf("Processing the common resource\n");
- Invoke the sleep method to make the first thread sleep for 5 seconds:
sleep(5);
- After a duration of 5 seconds, display the message First thread is over on the screen:
printf("First thread is over\n\n");
- The pthread_mutex_unlock function will be invoked, and the mutex object that we created in the first step will be passed to it to unlock it:
pthread_mutex_unlock(&lock);
- The thread function will be invoked by the second thread. Lock the mutex object again:
pthread_mutex_lock(&lock);
- To indicate that the second thread is running at the moment, display the message Second thread is running on the screen:
printf("Second thread is running\n");
- Again, to indicate that the common resource is being accessed by the thread, display the message Processing the common resource on the screen:
printf("Processing the common resource\n");
- Introduce a delay of 5 seconds. Then, display the message second thread is over on the screen:
sleep(5);
printf("Second thread is over\n\n");
- Unlock the mutex object:
pthread_mutex_unlock(&lock);
- Invoke the pthread_join method twice and pass the thread identifiers to it:
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
- Invoke the pthread_mutex_destroy method to destroy the mutex object:
pthread_mutex_destroy(&lock);
The twothreadsmutex.c program for creating two threads that share common resources is as follows:
#include<stdio.h>
#include<pthread.h>
#include<unistd.h>
pthread_t tid1,tid2;
pthread_mutex_t lock;
void* runThread(void *arg)
{
pthread_mutex_lock(&lock);
pthread_t id = pthread_self();
if(pthread_equal(id,tid1))
printf("First thread is running\n");
else
printf("Second thread is running\n");
printf("Processing the common resource\n");
sleep(5);
if(pthread_equal(id,tid1))
printf("First thread is over\n\n");
else
printf("Second thread is over\n\n");
pthread_mutex_unlock(&lock);
return NULL;
}
int main(void)
{
if (pthread_mutex_init(&lock, NULL) != 0)
printf("\n mutex init has failed\n");
pthread_create(&tid1, NULL, &runThread, NULL);
pthread_create(&tid2, NULL, &runThread, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_mutex_destroy(&lock);
return 0;
}
Now, let's go behind the scenes.