Mutex lock is the popular way of achieving the thread synchronization. A mutex is lock that we set before using a shared resource and release it after using it. When the lock is set no other thread can use the shared resource or the locked code of region.
The usual routine of using a Mutex Locks has the following steps
- Initialize a mutex using the pthread_mutex_init function
- Lock a mutex either using the pthread_mutex_lock function or with pthread_mutex_trylock function
- Unlock a mutex using the pthread_mutex_unlock function
- Finally destroy the mutex using the pthread_mutex_destroy function.
Mutex Initialization
pthread_mutex_init() initializes the mutex to its default value or to specific mutex attributes if they have been already set up with the pthread_mutexattr_init().
Syntax
int pthread_mutex_init(pthread_mutex_t *mp, const pthread_mutexattr_t *mattr);
- mp - pointer to the specific mutex
- mattr - pointer to the mutex attribute. The default value is NULL. The effect of mattr set to NULL is the same as passing the address of a default mutex attribute object, but without the memory overhead.
When the mutex is initialized, the mutex is in an unlocked state. Do not reinitialize or destroy a mutex lock while other threads are using the mutex. Program failure results if either action is not done correctly. If a mutex is reinitialized or destroyed, the application must be sure the mutex is not currently in use.
Return Values
pthread_mutex_init() returns zero after completing successfully. Any other return value indicates that an error occurred. When any of the following conditions occurs, the function fails and returns the corresponding value.
- EBUSY - The implementation has detected an attempt to reinitialize the object referenced by mp, a previously initialized but not yet destroyed mutex.
- EINVAL - The mattr attribute value is invalid. The mutex has not been modified.
- EFAULT - The address for the mutex pointed at by mp is invalid.
Use the macro PTHREAD_MUTEX_INITIALIZER to initialize statically defined mutexes to their default attributes.
Example
pthread_mutex_t mp = PTHREAD_MUTEX_INITIALIZER;
Mutex Locking
Use pthread_mutex_lock() to lock the mutex pointed to by mutex.
Syntax
int pthread_mutex_lock(pthread_mutex_t *mutex);
- mutex - When pthread_mutex_lock() returns, the mutex is locked.
The calling thread is the owner. If the mutex is already locked and owned by another thread, the calling thread blocks until the mutex becomes available.
If the mutex type is PTHREAD_MUTEX_NORMAL, deadlock detection is not provided. Attempting to relock the mutex causes deadlock. If a thread attempts to unlock a mutex not locked by the thread or a mutex that is unlocked, undefined behavior results.
If the mutex type is PTHREAD_MUTEX_ERRORCHECK, then error checking is provided. If a thread attempts to relock a mutex that the thread has already locked, an error is returned. If a thread attempts to unlock a mutex not locked by the thread or a mutex that is unlocked, an error is returned.
If the mutex type is PTHREAD_MUTEX_RECURSIVE, then the mutex maintains the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count is set to 1. Every time a thread relocks this mutex, the lock count is incremented by 1. Every time the thread unlocks the mutex, the lock count is decremented by 1. When the lock count reaches 0, the mutex becomes available for other threads to acquire. If a thread attempts to unlock a mutex not locked by the thread or a mutex that is unlocked, an error is returned.
If the mutex type is PTHREAD_MUTEX_DEFAULT, attempting to recursively lock the mutex results in undefined behavior. Attempting to unlock the mutex if the mutex was not locked by the calling thread results in undefined behavior. Attempting to unlock the mutex if the mutex is not locked results in undefined behavior.
Return Values
pthread_mutex_lock() returns zero after completing successfully. Any other return value indicates that an error occurred. When any of the following conditions occurs, the function fails and returns the corresponding value.
- EAGAIN - The mutex could not be acquired because the maximum number of recursive locks for mutex has been exceeded.
- EDEADLK - The current thread already owns the mutex
- EOWNERDEAD - The last owner of this mutex failed while holding the mutex. This mutex is now owned by the caller. The caller must attempt to make the state protected by the mutex consistent. If the caller is able to make the state consistent, call pthread_mutex_consistent_np() for the mutex and unlock the mutex. Subsequent calls to pthread_mutex_lock() behave normally. If the caller is unable to make the state consistent, do not call pthread_mutex_init() for the mutex. Unlock the mutex instead. Subsequent calls to pthread_mutex_lock() fail to acquire the mutex and return an ENOTRECOVERABLE error code. If the owner that acquired the lock with EOWNERDEAD fails, the next owner acquires the lock with EOWNERDEAD.
- ENOTRECOVERABLE - The mutex you are trying to acquire is protecting state left irrecoverable by the mutex’s previous owner that failed while holding the lock. The mutex has not been acquired.
- ENOMEM - The limit on the number of simultaneously held mutexes has been exceeded.