Cognitive Scaffold

Preparing your thinking workspace

arrow_back_ios_new
MENTAL MODEL · M6235

Mutex

Mutex
DecideHigh supportDecision Science
Included
account_tree

Updated 2026-07-28

Loading revision record…

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.

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/5

Practice this model in one real situation:

Check to track your progress (stored locally)
Learning progress0%

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.

  • link
    zh.wikipedia.orghttps://zh.wikipedia.org/wiki/%E4%BA%92%E6%96%A5%E9%94%81ZH · Explicit
    verified
account_treeGenealogyexpand_more

RELATED MODELS