Skip to content

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

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