简体   繁体   中英

pthread_mutex_trylock() in Linux

I am learning to use mutex in Linux programming. I came across trylock function, which first checks for the mutex if it is available it locks it otherwise, it return.

Now my question is:

  • Does it return after reaching the end of function without executing the critical section, when trylock is called?
  • Why doesn't it is printing errno in my code below?

Here is the code:

int main()
{
pthread_t tid[5];

int i;

if(pthread_mutex_init(&mutex,NULL))
    printf("Failed to lock the mutex\n");
for(i=0;i<5;i++)
{
    if(pthread_create(&tid[i],NULL,func,&i))
        printf("Failed to create a thread\n");

    if(errno==EBUSY)
        printf("thread busy\n");

}

for(i=0;i<5;i++)
{
    if(pthread_join(tid[i],NULL))
        printf("Failed to wait for thread %d\n",i);
}

printf("All threads terminated\n");

return 0;
}

void *func(void * arg)
{
int i=*(int *)arg;

if(pthread_mutex_trylock(&mutex)==0)
{
    sleep(5);
printf(" i is %d\n",i);

pthread_mutex_unlock(&mutex);
}
else
    if(errno== EBUSY)
            printf("thread busy\n");
}

Sorry for format less code..

Regards

pthread_mutex_trylock() doesn't set errno - you simply use the return value:

int result = pthread_mutex_trylock(&mutex);

if(result==0)
{
    sleep(5);
    printf(" i is %d\n",i);

    pthread_mutex_unlock(&mutex);
}
else
    if (result == EBUSY)
            printf("thread busy\n");
}

The documentation for pthread_mutex_trylock says it returns the error code if locking the mutex cannot be done, it doesn't set errno to EBUSY. To get the expected result, you should do eg

int rc;
if((rc = pthread_mutex_trylock(&mutex))==0) {
  sleep(5);
  printf(" i is %d\n",i);

  pthread_mutex_unlock(&mutex);
} else {
  printf("thread busy: %s\n", strerror(rc));
}

Does it return after reaching the end of function without executing the critical section, when trylock is called?

if pthread_mutex_trylock(&mutex) is successfull (ie it returns 0) then first part (ie critical section) would be executed and function will return. if pthread_mutex_trylock(&mutex) is unsuccessful(ie it returns -1) then else part would be executed and hopefully thread busy would be printed and function would return without executing critical section.

Why doesn't it is printing errno in my code below?

Either the call to pthread_mutex_trylock(&mutex) is always successful, if you see all values of i (ie 1-5) being printed then this is the case. Or the errorno is not EBUSY you can check that by printing something before if(errno== EBUSY) .

No Nos. No. You have confused return value, with errno. Errno, is handled in threads via the precompiler doing (*errnoAddress), so, yes, there is a errno per thread. You should also NEVER use strerror(), as it's a security hole. That is another discussion. But, here is your code using errno.

#include <errno.h> /* usually */ 
int rc;
if((rc = pthread_mutex_trylock(&mutex))==0) {
  sleep(5);
  printf(" i is %d\n",i);
  pthread_mutex_unlock(&mutex);
} else {
  int e = errno; 
  printf("thread busy: %.64s\n", strerror(e)); 
  /* strerror is a security hole, print %d, and call it done.   */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM