Using pthread_mutex_trylock

In the mutex we saw in the post ” ” , if a mutex is locked by one thread, other threads will wait infinitesly for the mutex when pthread_mutex_lock is called. The other threads wil get out pf hte wait only when the mutex becomes available or the process is killed.
If such a behviour of infinite wait is undesirable we can use the variant pthread_mutex_trylock.

In this function the thread will try to lock the mutex, but in case the mutes in not available, it will return with an error EBUSY and will not try to acuire the lock again . If it is able to lock the mutex, the return value is 0 else it returns a non zero error value.

The following code shows the use of the function pthread_mutex_trylock. In the program, two threads are created one to write to a file and another to read from the file. The read thread is started with a little delay to allow the write thread to get access of the mutex.

Both the thread lock the muetx using pthread_mutex_trylock. The write thread is able to get access to the lock and continues with the execution of the thread. The read thread on the other hand tries to access the lock but is unaalbe to get a lock on the mutex as the write thread is holding the same. Thus it returns with an error value, because of which the thread has to be exited with our reading the file.

#include #include #include pthread_mutex_t rw_mutex; void * write(void *temp) { char *ret; FILE *file1; char *str; int retur; retur=pthread_mutex_trylock(&rw_mutex); if(retur!=0) { printf(“Unable to get lock”); } printf(“\nFile locked, please enter the message \n”); str=(char *)malloc(10*sizeof(char)); file1=fopen(“temp”,”w”); scanf(“%s”,str); fprintf(file1,”%s”,str); fclose(file1); sleep(5); pthread_mutex_unlock(&rw_mutex); printf(“\nUnlocked the file you can read it now \n”); return ret; } void * read(void *temp) { char *ret; FILE *file1; char *str; int retur; sleep(5); retur=pthread_mutex_trylock(&rw_mutex); if(retur!=0) { printf(“Unable to getlock”); return retur; } printf(“\nOpening file for reading\n”); file1=fopen(“temp”,”r”); str=(char *)malloc(10*sizeof(char)); fscanf(file1,”%s”,str); printf(“\nMessage from file is %s \n”,str); fclose(file1); pthread_mutex_unlock(&rw_mutex); return ret; } main() { pthread_t thread_id,thread_id1; pthread_attr_t attr; int ret; void *res; ret=pthread_create(&thread_id,NULL,&write,NULL); ret=pthread_create(&thread_id1,NULL,&read,NULL); pthread_join(thread_id,&res); pthread_join(thread_id1,&res); }

Save the file as mutex_trylock.c, and compile it using the flab -lpthread

$ cc -lpthread -o mutex_trylock mutex_trylock.c $ ./mutex_trylock File locked, please enter the message Hello Unable to getlock Unlocked the file you can read it now

We can see from the messages that the read thread has returned with an error and was unable to open the file as the pthread_mutex_trylock failed to get the lock.

Tags: , ,
Copyright 2017. All rights reserved.

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