using pthread_mutex_timedlock in linux.

We saw in the post ” ” how we can use mutexes in pthreads to achieve synchronization among various threads. Along with the the three functions we saw, there is a fourth mutex function that we can use when we want to wait on a mutex for only a specific duration and then continue irrespective of whether we are able to get a lock on the mutex or not. The function for timed operation on mutex it pthread_mutex_timedlock whose syntax is

int pthread_mutex_timedlock(pthread_mutex_t *restrict mutex,const struct timespec *restrict abs_timeout);

The timeout is the time for which we want to wait on the mutex. To specify the timeout we need to use the structure timepsec which has two fields one for seconds and other for nanoseconds as shown below.

struct timespec { __time_t tv_sec; /* Seconds. */ long int tv_nsec; /* Nanoseconds. */ };

The mutex if available will be locked immediately, if the mutex is not available the thread will wait for the duration of time mentioned in the abs_timeout and then exit from the wait.

The following program shows an example of using the pthread_mutex_timedlock. In the program thread1 takes a value as input from the user and assigns it to a variable “temp”. In thread2 the same variable is accessed and its value is printed. Thread1 locks a mutex while getting the value for temp from the user, but thread2 waits only for 5 seconds for the mutex to get unlocked after which it exits with a time out message and does not print the value of the variable temp.

To mention the timeout we need to first allocate memory for a timespec structure

struct timespec *wait wait=(struct timespec *)(malloc(sizeof(struct timespec)); wait->tv_sec=5; wait->tv_nsec=0;

Thus now we have a timespec structure with 5 seconds and 0 nanoseconds as the time values, which can be passed to the function pthread_mutex_timedlock to achieve a 5 second timeout.
The whole function looks as follows

#include#include#include#include pthread_mutex_t mutex1,mutex2; #define perror(s) printf(“\n” s “\n”); int temp; void * func1(){ pthread_mutex_lock(&mutex1); printf(“\nEnter a value for temp\n”); scanf(“%d”,&temp); pthread_mutex_unlock(&mutex1); } void * func2() { struct timespec *wait; int ret; wait=(struct timespec *)(malloc(sizeof(struct timespec))); wait->tv_sec=5; wait->tv_nsec=0; sleep(5); ret=pthread_mutex_timedlock(&mutex1,wait); if(ret!=0){ perror(“Wait timed out”); return ; } printf(“\nvalue assigned for temp is %d\n”,temp); pthread_mutex_unlock(&mutex1); } main() { int ret; pthread_t thread1,thread2; ret=pthread_create(&thread1,NULL,&func1,NULL); if(ret!=0){ perror(“Unable to create thread”); } ret=pthread_create(&thread2,NULL,&func2,NULL); if(ret!=0){ perror(“Unable to create thread”); } printf(“Created threads”); pthread_join(thread1,NULL); pthread_join(thread2,NULL); }

Save the program as mutex_timedlock.c, and compile it with the flag -lpthread

$ cc -lpthread -o mutex_timedloc mutex_timedlock.c

While executing the program when the prompt for entering a value for the variable appears, if you wait for more than 5 seonds for entering the value, you will noticed the timed out message from the second thread will appear on the other hand if we enter the value with in 5 seconds the value entered is printed by the second thread. case1: Waiting for more than 5 seconds before entering the value

$ ./mutex_timedlock Created threads Enter a value for temp Wait timed out 5

Created threads Enter a value for temp 5 value assigned for temp is 5

Thus we are able to achieve a mutex lock for a specific duration using the pthread_mutex_timedlock function.

Tags: , ,
Copyright 2017. All rights reserved.

Posted January 31, 2013 by Tux Think in category "Linux