A race condition occurs when the outcome depends on the unpredictable timing of concurrent accesses to shared data, for example two threads doing counter = counter + 1, which is a read-modify-write sequence that can lose updates.
The critical section is the code that accesses shared resources. A correct solution needs mutual exclusion (only one at a time), progress (a process outside the section cannot block others from entering), and bounded waiting (no process waits forever).
Solutions include locks (mutexes), semaphores, monitors, atomic hardware instructions (test-and-set, compare-and-swap), and Peterson's algorithm for two processes.
Loading…