Skip to content

Mutual exclusion

Category: Synchronization · Status: stub · Lessons: chapter 02, Shared state

One line: The guarantee that at most one task is inside a critical section at any moment.

Also called: exclusive access.

How it connects

flowchart LR
  n_data_race["Data race"]
  n_mutex["Mutex"]
  n_mutual_exclusion["Mutual exclusion"]
  n_race_condition["Race condition"]
  n_synchronization["Synchronization"]
  n_toctou["Time of check to time of use"]
  n_mutex -->|is a| n_mutual_exclusion
  n_mutual_exclusion -->|is a| n_synchronization
  n_mutual_exclusion -->|prevents| n_data_race
  n_mutual_exclusion -->|prevents| n_race_condition
  n_mutual_exclusion -->|prevents| n_toctou
  classDef center stroke-width:3px
  class n_mutual_exclusion center
  classDef outside stroke-dasharray: 4 3
  class n_data_race,n_mutex,n_race_condition,n_synchronization,n_toctou outside

In each language

Rust Enforced by types: a Mutex<T> owns its data, reachable only through the guard that lock returns
Go The memory model ↗: programs must serialize access to shared data, with channel operations or the sync and sync/atomic primitives
JavaScript The Web Locks API ↗ lets a script in one tab or worker hold a named lock while it works
Erlang and Elixir Little shared memory to exclude: all data in messages between processes is copied ↗, except refc binaries and literals

Where to read more