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 (i.e. it returns 0) then first part (i.e. critical section) would be executed and function will return. if pthread_mutex_trylock(&mutex)
is unsuccessful(i.e. 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 (i.e. 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)
.