plibsys 0.0.5
|
Mutex routines. More...
Go to the source code of this file.
Typedefs | |
typedef struct PMutex_ | PMutex |
Mutex opaque data structure. | |
Functions | |
P_LIB_API PMutex * | p_mutex_new (void) |
Creates a new PMutex object. | |
P_LIB_API pboolean | p_mutex_lock (PMutex *mutex) |
Locks a mutex. | |
P_LIB_API pboolean | p_mutex_trylock (PMutex *mutex) |
Tries to lock a mutex immediately. | |
P_LIB_API pboolean | p_mutex_unlock (PMutex *mutex) |
Releases a locked mutex. | |
P_LIB_API void | p_mutex_free (PMutex *mutex) |
Frees PMutex object. | |
Mutex routines.
A mutex is a mutual exclusive (hence mutex) synchronization primitive which allows access to a critical section only to one of the concurrently running threads. It is used to protected shared data structures from concurrent modifications which could lead to unpredictable behavior.
When entering a critical section a thread must call p_mutex_lock() to get a lock. If another thread is already holding the lock all other threads will be suspended until the lock is released with p_mutex_unlock(). After releasing the lock one of the waiting threads is resumed to continue execution. On most systems it is not specified whether a mutex waiting queue is fair (FIFO) or not.
The typical mutex usage:
You can also think of the mutex as a binary semaphore.
It is implementation dependent whether recursive locking or non-locked mutex unlocking is allowed, but such actions can lead to unpredictable behavior. Do not rely on such behavior in cross-platform applications.
This is the thread scoped mutex implementation. You could not share this mutex outside the process adress space, but you can share it between the threads of the same process.
Definition in file pmutex.h.
Locks a mutex.
mutex | PMutex to lock. |
Forces the calling thread to sleep until mutex becomes available for locking.
Tries to lock a mutex immediately.
mutex | PMutex to lock. |
Tries to lock mutex and returns immediately if it is not available for locking.
Releases a locked mutex.
mutex | PMutex to release. |
If mutex was previously locked then it becomes unlocked.
It's implementation dependent whether only the same thread can lock and unlock the same mutex.