On this page
mtx_lock
Prev: thrd_create | Up: threads.h | Next: setjmp.h
Rel: threads.h, Concurrency vs. Parallelism
Ref: cppreference — mtx_lock
Tags: #public #prog #c #stdlib #threads #tier/specialized
Include
#include <threads.h>
Signature & Overview
int mtx_lock(mtx_t *mutex); /* + mtx_init, mtx_unlock, mtx_destroy */
Acquires a mutual-exclusion lock to guard shared mutable state across threads.
Notes & Pitfalls
- C11 threads (
<threads.h>): Not supported on macOS (use<pthread.h>on POSIX); always pairmtx_lock()withmtx_unlock().
Example
// C11 <threads.h> pattern (Linux/glibc 2.28+):
// mtx_t m; mtx_init(&m, mtx_plain);
// mtx_lock(&m); /* critical section */ mtx_unlock(&m);
// mtx_destroy(&m);
Linked from
Related notes
- tuts/c/stdlib/threads/thrd_create Linked here; links to this page; shared tags: #c, #prog, #stdlib, #threads, #tier
- tuts/c/stdlib/threads Linked here; links to this page; shared tags: #c, #prog, #stdlib, #threads
- tuts/c/stdlib/setjmp Linked here; shared tags: #c, #prog, #stdlib
- tuts/programming/concurrency/concurrency_vs_parallelism Linked here; shared tags: #prog
- tuts/c/stdlib/assert/assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/assert/static_assert shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isalpha shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isdigit shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/isspace shared tags: #c, #prog, #stdlib, #tier
- tuts/c/stdlib/ctype/tolower shared tags: #c, #prog, #stdlib, #tier