mtx_lock, mtx_timedlock, mtx_trylock, mtx_unlock — lock and unlock a mutex
#include <threads.h>
int mtx_lock(mtx_t *mtx);
int mtx_timedlock(mtx_t *restrict mtx,
const struct timespec *restrict ts);
int mtx_trylock(mtx_t *mtx);
int mtx_unlock(mtx_t *mtx);
[CX]The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional. This volume of POSIX.1-2024 defers to the ISO C standard.
The mtx_lock() function shall block until it locks the mutex pointed to by mtx. If the mutex is non-recursive, the application shall ensure that it is not already locked by the calling thread.
The mtx_timedlock() function shall block until it locks the mutex pointed to by mtx or until after the TIME_UTC-based calendar time pointed to by ts. The application shall ensure that the specified mutex supports timeout. [CX]
Under no circumstance shall the function fail with a timeout if the mutex can be locked immediately. The validity of the ts parameter need not be checked if the mutex can be locked immediately.
The mtx_trylock() function shall endeavor to lock the mutex pointed to by mtx. If the mutex is already locked (by any thread, including the current thread), the function shall return without blocking. If the mutex is recursive and the mutex is currently owned by the calling thread, the mutex lock count (see below) shall be incremented by one and the mtx_trylock() function shall immediately return success.
[CX]
These functions shall not be affected if the calling thread executes a signal handler during the call; if a signal is delivered to a thread waiting for a mutex, upon return from the signal handler the thread shall resume waiting for the mutex as if it was not interrupted.
If a call to mtx_lock(), mtx_timedlock() or mtx_trylock() locks the mutex, prior calls to mtx_unlock() on the same mutex shall synchronize with this lock operation.
The mtx_unlock() function shall unlock the mutex pointed to by mtx. The application shall ensure that the mutex pointed to by mtx is locked by the calling thread. [CX]
If there are threads blocked on the mutex object referenced by mtx when mtx_unlock() is called, resulting in the mutex becoming available, the scheduling policy shall determine which thread shall acquire the mutex.
A recursive mutex shall maintain the concept of a lock count. When a thread successfully acquires a mutex for the first time, the lock count shall be set to one. Every time a thread relocks this mutex, the lock count shall be incremented by one. Each time the thread unlocks the mutex, the lock count shall be decremented by one. When the lock count reaches zero, the mutex shall become available for other threads to acquire.
For purposes of determining the existence of a data race, mutex lock and unlock operations on mutexes of type mtx_t behave as atomic operations. All lock and unlock operations on a particular mutex occur in some particular total order.
If mtx does not refer to an initialized mutex object, the behavior of these functions is undefined.
The mtx_lock() and mtx_unlock() functions shall return thrd_success on success, or thrd_error if the request could not be honored.
The mtx_timedlock() function shall return thrd_success on success, or thrd_timedout if the time specified was reached without acquiring the requested resource, or thrd_error if the request could not be honored.
The mtx_trylock() function shall return thrd_success on success, or thrd_busy if the resource requested is already in use, or thrd_error if the request could not be honored. The mtx_trylock() function can spuriously fail to lock an unused resource, in which case it shall return thrd_busy.
See RETURN VALUE.
None.
None.
These functions are not affected by signal handlers for the reasons stated in XRAT B.2.3 Error Numbers.
Since <pthread.h> has no equivalent of the mtx_timed mutex property, if the <threads.h> interfaces are implemented as a thin wrapper around <pthread.h> interfaces (meaning mtx_t and pthread_mutex_t are the same type), all mutexes support timeout and mtx_timedlock() will not fail for a mutex that was not initialized with mtx_timed. Alternatively, implementations can use a less thin wrapper where mtx_t contains additional properties that are not held in pthread_mutex_t in order to be able to return a failure indication from mtx_timedlock() calls where the mutex was not initialized with mtx_timed.
None.
XBD <threads.h>
First released in Issue 8. Included for alignment with the ISO/IEC 9899:2018 standard.
return to top of page
结肠炎吃什么药治疗效果好 | 闰年是什么 | 地龙是什么 | 扩胸运动有什么好处 | 鹧鸪读音是什么 |
手心脚心发热吃什么药 | 体罚是什么意思 | 带状疱疹后遗神经痛挂什么科 | 6岁属什么生肖 | 无名指长痣代表什么 |
半夜会醒是什么原因 | 漂亮的近义词是什么 | 产厄是什么意思 | 7月份是什么星座 | 肩周炎口服什么药最好 |
2003年什么年 | 鸡飞狗跳是什么生肖 | 一岁半打什么疫苗 | 氧化剂是什么 | 用盐洗脸有什么好处 |
吃鱼眼睛有什么好处ff14chat.com | 金蝉吃什么hcv8jop4ns8r.cn | 老公的姐姐叫什么hcv9jop0ns2r.cn | 什么牌子的床垫好jinxinzhichuang.com | 早上起床手指肿胀是什么原因hcv8jop4ns1r.cn |
老打饱嗝是什么原因hcv9jop7ns3r.cn | 保泰松是什么药hcv7jop7ns1r.cn | 属相兔和什么属相最佳hcv9jop8ns2r.cn | dazzling什么意思hcv8jop7ns7r.cn | 六安瓜片属于什么茶hcv8jop9ns4r.cn |
笏是什么意思hcv9jop2ns2r.cn | 尿常规阳性是什么意思hcv8jop0ns6r.cn | 一张张什么wuhaiwuya.com | 黄褐斑是什么样的图片jiuxinfghf.com | 碘酒是什么hcv8jop4ns0r.cn |
虎属什么五行sanhestory.com | 为什么会得肿瘤hcv9jop3ns5r.cn | 痰多吃什么好化痰hcv8jop4ns5r.cn | 腿毛旺盛是什么原因hcv9jop4ns9r.cn | 丙辰是什么时辰hcv9jop0ns8r.cn |