Multithreaded Programming Guide

pthread_mutex_trylock(3T)

Use pthread_mutex_trylock() to attempt to lock the mutex pointed to by mp.

Prototype:
int	pthread_mutex_trylock(pthread_mutex_t *mp); 
#include <pthread.h>

pthread_mutex_t mp;
int ret;

ret = pthread_ mutex_trylock(&mp); /* try to lock the mutex */

pthread_mutex_trylock() is a nonblocking version of pthread_mutex_lock(). When the mutex is already locked, this call returns with an error. 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.


EBUSY

The mutex pointed to by mp was already locked.


EINVAL

The value specified by mp does not refer to an initialized mutex object.