Attempts to lock the specified mutex. If the mutex is already locked, the calling thread does not wait for the mutex to become available.
1 – C Binding
#include <pthread.h> int pthread_mutex_trylock ( pthread_mutex_t *mutex);
2 – Arguments
mutex Mutex to be locked.
3 – Description
This routine attempts to lock the mutex specified in the mutex argument. When a thread calls this routine, an attempt is made to immediately lock the mutex. If the mutex is successfully locked, this routine returns zero (0) and the calling thread becomes the mutex's current owner. If the specified mutex is locked when a thread calls this routine, the calling thread does not wait for the mutex to become available. The behavior of this routine is as follows: o For a normal, default, or errorcheck mutex: if the mutex is locked by any thread (including the calling thread) when this routine is called, this routine returns [EBUSY] and the calling thread does not wait to acquire the lock. o For a normal or errorcheck mutex: if the mutex is not owned, this routine returns zero (0) and the mutex becomes locked by the calling thread. o For a recursive mutex: if the mutex is owned by the current thread, this routine returns zero (0) and the mutex lock count is incremented. (To unlock a recursive mutex, each call to pthread_mutex_trylock() must be matched by a call to pthread_ mutex_unlock().) Use the pthread_mutexattr_settype() routine to set the mutex type attribute (normal, default, recursive, or errorcheck).
4 – Return Values
If an error condition occurs, this routine returns an integer value indicating the type of error. Possible return values are as follows: Return Description 0 Successful completion. [EBUSY] The mutex is already locked; therefore, it was not acquired. [EINVAL] The value specified by mutex is not a valid mutex.
5 – Associated Routines
pthread_mutexattr_settype() pthread_mutex_destroy() pthread_mutex_init() pthread_mutex_lock() pthread_mutex_unlock()