Name | Description | Notes | Source | Availability | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
mtx_ |
Destroy a mutex | ? | (·) | <threads.h> |
C11 | ||||||
mtx_ |
Initialize a mutex | ? | (·) | <threads.h> |
C11 | ||||||
mtx_ |
Lock a mutex | ? | (·) | <threads.h> |
C11 | ||||||
mtx_ |
Type of non-recursive, non-timeout mutex | ? | <threads.h> |
C11 | |||||||
mtx_ |
Type of mutex supporting recursion | ? | <threads.h> |
C11 | |||||||
mtx_t |
Mutex-identifier type | ? | T | <threads.h> |
C11 | ||||||
mtx_ |
Type of mutex supporting timeout | ? | <threads.h> |
C11 | |||||||
mtx_ |
Try for some time to lock a mutex | ? | (·) | <threads.h> |
C11 | ||||||
mtx_ |
Try to lock a mutex | ? | (·) | <threads.h> |
C11 | ||||||
mtx_ |
Unlock a mutex | ? | (·) | <threads.h> |
C11 |
Names matching ^mtx_[a-z]
might be added to <threads.h>
.
A mutex is a construct that prevents two threads from accessing shared data at the same time. When a thread intends to access shared data, it must try to claim the lock on the mutex associated with that data. Only one thread can own the lock, and the other threads will block when trying to claim it, until the original thread releases the lock. At that point, one of the blocked threads will be chosen arbitrarily, unblock, and claim the lock.
<threads.h>
defines a type
mtx_t
to represent a mutex.
#include<threads.h>
int mtx_init (mtx_t *mp, int ty); void mtx_destroy (mtx_t *mp);
A mutex m
must be initialized with
mtx_
before being used by any other function.
ty
must be either mtx_
for a mutex that supports timed locks with mtx_
,
or
mtx_
for one that does not have to.
cy
can be bitwise-ORed with
mtx_
for a mutex that supports recursive locking.
Value of ty |
Timed | Recursive |
---|---|---|
mtx_ |
||
mtx_ |
yes | |
mtx_ |
yes | |
mtx_ |
yes | yes |
mtx_
returns thrd_
on success, or
thrd_
on failure.
Resources allocated for m
by
mtx_
should be released with mtx_
.
#include<threads.h>
int mtx_lock (mtx_t *mp); int mtx_timedlock (mtx_t *mp, const struct timespec *tp); int mtx_trylock (mtx_t *mp);
These functions attempt to claim the lock of mutex
*mp
. If the mutex is already locked by
another thread, mtx_
blocks until the mutex is unlocked, returning thrd_
. mtx_
also blocks until then, returning thrd_
, or until the duration
specified by *tp
has passed, returning
thrd_
. mtx_
does not block, but returns thrd_
if it cannot claim the
lock. These functions also block or fail respectively if the
lock is claimed by the current thread, and the mutex was not
initialized with mtx_
.
These functions return thrd_
if the lock could not be
claimed for some other reason.
mtx_
cannot be called on a mutex that was not initialized with
mtx_
.
#include<threads.h>
int mtx_unlock (mtx_t *mp);
mtx_
unlocks the mutex *mp
, returning
thrd_
on success, and
thrd_
on failure. If any other
threads are blocked in mtx_
or mtx_
on *mp
, one of them is chosen
arbitrarily, unblocks, and claims the lock.
If the mutex was initialized with mtx_
,
and has been locked n times without being
unlocked, it will have to be unlocked n times
before other threads are unblocked.