Mutex
Updated 2026-07-28
Summary
A mutex (short for mutual exclusion) is a mechanism used in multithreaded programming to prevent two threads from simultaneously reading and writing to the same shared resource (such as a global variable). This is achieved by dividing the code into critical sections. A critical section is a block of code that accesses a shared resource; it is not a mechanism or algorithm. A program, process, or thread can have multiple critical sections, but they may not necessarily use a mutex.
Definition
A mutex (short for mutual exclusion) is a mechanism used in multithreaded programming to prevent two threads from simultaneously reading and writing to the same shared resource (such as a global variable). This is achieved by dividing the code into critical sections. A critical section is a block of code that accesses a shared resource; it is not a mechanism or algorithm. A program, process, or thread can have multiple critical sections, but they may not necessarily use a mutex. Examples of resources that require this mechanism include flags, queues, counters, interrupt handlers, and other resources used to pass data or synchronize state among multiple concurrently running code paths. Maintaining synchronization, consistency, and integrity of these resources is difficult because a thread can be paused (suspended) or resumed (awakened) at any moment. For example, code (A) is modifying a piece of data step by step. At that moment, another thread (B) is awakened for some reason.
Introduction
How to use
A mutex (short for mutual exclusion) is a mechanism used in multithreaded programming to prevent two threads from simultaneously reading and writing to the same shared resource (such as a global variable). This is achieved by dividing the code into critical sections. A critical section is a block of code that accesses a shared resource; it is not a mechanism or algorithm. A program, process, or thread can have multiple critical sections, but they may not necessarily use a mutex.
Mechanism
A mutex is a binary semaphore that ensures a critical resource is held by only one execution unit at a time; non-holders block and wait, thereby avoiding data races and inconsistencies caused by concurrent reads and writes.
Practice
In progress 0/5Practice this model in one real situation:
Cases
- Two threads simultaneously modify the same account balance; use a mutex to ensure the total is correct; in an organization, assign a single owner to core decisions to prevent multiple people from arbitrarily changing instructions.
Common pitfalls
- Forgetting to unlock leads to deadlock; lock granularity too large slows concurrency; inconsistent lock ordering causes deadlock; treating mutex as a panacea while ignoring that lock-free designs may be better.
Sources
Source support: Explicit
References currently contain only Wikipedia links; primary sources still need review.
- zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E4%BA%92%E6%96%A5%E9%94%81verified
account_treeGenealogyexpand_more
PRIVATE NOTES · Only visible to you
SAVED Q&A
ENTRY Q&A · Private saving available
Ask with a clear boundary
thinkingmodels answers from published entry context only.
Your question is sent to thinkingmodels. The answer uses public entry context only.
RELATED MODELS