Race conditions and critical sections
Reason about shared updates by tracing operations, not by assuming source lines are atomic.
What you’ll learn
- Construct a lost-update interleaving
- Identify the shared invariant
- Choose a synchronization boundary
Shared state creates an ordering question
Suppose two workers update one shared counter. The intended result after two increments is an increase of two. A source expression that looks like one update may involve reading a value, computing a new value, and writing it back. Unless the language and execution model guarantee the required atomic behavior, those steps can interleave with another worker's steps. The important question is which ordering of operations is permitted and whether every permitted ordering preserves the intended result.
Trace a lost update
Start with the counter at zero. Worker A reads zero, and worker B reads zero before either writes. A computes and writes one; B also computes and writes one. Two increments have occurred from the workers' perspective, but the shared value is only one. This small trace demonstrates a race affecting correctness. Timing changes may make the symptom disappear during a demonstration, but an intermittent success does not establish that the program has become safe.
Initial shared count: 0
Worker A reads count: 0
Worker B reads count: 0
Worker A computes and writes: 1
Worker B computes and writes: 1
Two increments were attempted, but the final count is 1.
Protect the entire read-modify-write operation.Protect the complete operation
A critical section is the part of the program that accesses shared state under a synchronization rule. A mutex can make the read-modify-write sequence exclusive when all relevant access follows the same locking discipline. Protecting only the final write is insufficient if the decision was based on an earlier unprotected read. State the invariant and the set of operations that must be coordinated. A lock's existence is not enough when some callers bypass it or different callers protect the same state with unrelated locks.
Choose an appropriate mechanism
An atomic operation may be suitable for a simple counter, while a lock or another coordination mechanism may be needed for a rule involving several related fields. The available guarantees depend on the language and platform. Locks can introduce contention and deadlocks, so keep ownership and acquisition order understandable. Avoid replacing a real synchronization protocol with sleeps: waiting for an arbitrary duration does not establish that another worker has completed the necessary action.
Test the model and the behavior
Write down the unsafe interleaving before trying to fix it. Then explain why the chosen synchronization removes that interleaving or makes it harmless. Stress tests and race-detection tools can provide useful evidence, but they do not prove that every possible schedule was exercised. Include shutdown and error paths because a worker that exits while holding a resource can block others. The strongest explanation combines a clear state model, a synchronization contract, and targeted observations from tests.
Try it yourself
Why is it insufficient to lock only the assignment in a read-compute-write update?
Show a possible solution
Another worker can change the shared value after the unprotected read but before the protected write. The update can still overwrite a newer result. Coordinate the full operation that depends on the shared state, or use an atomic operation with the required semantics.
Check your understanding
Try 14 questions across operating systems. Read each explanation and revisit anything that needs another pass.
Practise this subjectGo to the source
Use these primary references to check details and explore the topic further.
Saved in this browser. Signed-in progress syncs to your account.