Critical section¶
Category: Synchronization · Status: stub · Lessons: chapter 02, Shared state
One line: A stretch of code that touches shared state and must not be run by two tasks at once.
Also called: critical region.
How it connects¶
- See also: Mutual exclusion
In each language¶
| Rust | As long as the MutexGuard ↗ lives: the mutex is unlocked when the guard is dropped |
| Go | Between Mutex.Lock ↗ and Unlock; a locked Mutex is not tied to the goroutine that locked it |
| C++ | The scope of a std::lock_guard ↗, which owns the mutex for the duration of a scoped block |
| Java | A synchronized statement ↗: the monitor is unlocked whether the block completes normally or abruptly |
| Python | A with lock: block: threading primitives ↗ acquire on entry and release on exit |
| C# | The body of a lock statement ↗, released even if an exception is thrown |
| The operating system | Windows names an object after it: critical section objects ↗ work like a mutex for the threads of one process |
Where to read more¶
- In this library: Keeping every update
- In a sibling library: Go: A mutex guards a counter ↗
- In the books: Modern Multithreading, Richard H. Carver, Kuo-Chung Tai — ch. 2, 'The Critical Section Problem'
- In the books: Learn Concurrent Programming with Go, James Cutajar — ch. 4, 'Synchronization with mutexes' → 'Protecting critical sections with mutexes'
- In the books: Programming with POSIX Threads, David R. Butenhof — ch. 3, 'Synchronization' → 'Invariants, critical sections, and predicates'
- In the books: Python Asyncio Jump-Start, Jason Brownlee — ch. 5, 'Queues and Synchronization Primitives' → 'How to Protect Critical Sections with a Mutex Lock'
- In the books: The Art of Concurrency, Clay Breshears — ch. 3, 'Proving Correctness and Measuring Performance' → 'Example: The Critical Section Problem'
- In the books: The Art of Multiprocessor Programming, Maurice Herlihy, Nir Shavit — ch. 2, 'Mutual Exclusion' → 'Critical Sections'
- Reference: Wikipedia: Critical section ↗