Multithreaded Programming Guide


Use pthread_mutex_trylock(3T) to attempt to lock the mutex pointed to by mutex. (For Solaris threads, see "mutex_trylock(3T)".)

int	pthread_mutex_trylock(pthread_mutex_t *mutex); 
#include <pthread.h>

pthread_mutex_t mutex;
int ret;

ret = pthread_mutex_trylock(&mutex); /* try to lock the mutex */

pthread_mutex_trylock() is a nonblocking version of pthread_mutex_lock(). If the mutex object referenced by mutex is currently locked (by any thread, including the current thread), the call returns immediately. Otherwise, the mutex is locked and the calling thread is the owner.

Return Values

pthread_mutex_trylock() returns zero after completing successfully. Any other returned value indicates that an error occurred. When any of the following conditions occur, the function fails and returns the corresponding value.


The mutex could not be acquired because the mutex pointed to by mutex was already locked.


The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.