FREE LESSON · Operating systems & runtimes · 4 OF 4
Concurrency, Atomicity and Deadlocks
OS mastery: concurrency without corruption — Atomicity, waiting, deadlock, and isolation
Shared mutable state creates executions you did not write linearly.
An operation that looks single in source may compile into several loads and stores that interleave. Locks establish mutual exclusion; condition variables coordinate state changes; atomics provide specified indivisible operations and ordering. Deadlock can arise when tasks wait in a cycle for resources held by one another.
Correct concurrent code specifies allowed interleavings and the ordering that makes them safe.
Locks protect invariants, not lines of code
For a bounded queue, the useful invariant connects buffer contents, head, tail, and count. A lock protects the transition that changes those fields together. Producers wait while full; consumers wait while empty; state changes signal waiters. Locking one field but not the invariant still permits impossible combined states.
Name the invariant before choosing the synchronization primitive.