A critical region is code that accesses shared state and must be synchronized so concurrent threads cannot perform conflicting operations simultaneously. Mutual exclusion allows only one thread to execute the region at a time, preventing race conditions, corruption, inconsistent data, and crashes. section 8.1 section 8.1.1
A shared counter illustrates the boundary: reading or updating the counter belongs inside the protected region, while unrelated work should remain outside it. A mutex or spinlock guards entry; a thread must acquire the lock before accessing the resource. section 8.1 section 8.1.1
Use a critical region safely and efficiently:
- Protect every access to the shared resource with the same lock. section 8.1
- Acquire the mutex before entering, perform the required update, and release it immediately afterward. section 8.1.1
- Keep the region short: blocked threads may enter an OS wait state, adding latency. section 8.1.1
- Watch for convoying when contention, long regions, or poor scheduling make threads queue behind a delayed lock holder. section 8.2.3
A synchronized counter
In Rust, wrap a counter in std::sync::Mutex. Each of 100 spawned threads calls lock(), increments the counter inside the critical region, and releases the mutex when the guard leaves scope. The shared update is serialized while unrelated thread execution remains concurrent. section 8.1








