A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

The Critical Section Problem and Race Conditions

College Depth 98 in the knowledge graph ☐ I know this ☆ Set as goal
43topics build on this
363prerequisites beneath it
See this on the map →
Threads and ConcurrencyInter-Process Communication (IPC)+1 more→→Atomic Operations and Compare-and-SwapBarrier Synchronization Primitives+3 more
race-condition critical-section mutual-exclusion atomicity

Core Idea

A race condition occurs when two or more concurrent threads access shared data and the final result depends on the unpredictable interleaving of their operations. The critical section is the code segment where shared data is accessed; correct concurrent programs must ensure that only one thread executes its critical section at a time (mutual exclusion), that waiting threads eventually enter their critical section (progress and bounded waiting), and that no assumptions are made about CPU speed or scheduling. These three requirements — mutual exclusion, progress, bounded waiting — define the Critical Section Problem.

How It's Best Learned

Reproduce a race condition: have two threads increment a shared counter 1,000,000 times without synchronization and observe the incorrect final value. Then explain why the assembly-level read-modify-write sequence is not atomic.

Common Misconceptions

Explainer

From your study of threads and concurrency, you know that multiple threads can execute simultaneously and share the same memory space. This sharing is what makes threads powerful — but it also introduces a fundamental problem. When two threads read and write the same variable without coordination, the result depends on the exact timing of their operations. This unpredictable dependence on timing is called a race condition, and it is the central problem that all synchronization mechanisms exist to solve.

Consider a simple example: two threads each incrementing a shared counter by 1. The operation `counter = counter + 1` looks atomic in source code, but at the hardware level it is three steps — load the value from memory into a register, add 1, store the result back. If Thread A loads the value 5, then Thread B also loads 5 before A stores its result, both threads compute 6 and store 6. The counter should be 7 but ends up as 6. Run this millions of times and the final value will be wrong by an unpredictable amount, different on every run. This is why race conditions are so dangerous: programs that appear correct can fail intermittently and unreproducibly.

The code segment where a thread accesses shared data is called its critical section. The Critical Section Problem asks: how do we ensure correctness when multiple threads have critical sections that access the same data? The solution must satisfy three properties. Mutual exclusion requires that when one thread is in its critical section, no other thread can be in its critical section for the same data. Progress requires that if no thread is in the critical section and some threads want to enter, the selection of which thread enters next cannot be postponed indefinitely — the system must not deadlock. Bounded waiting requires that after a thread requests entry, there is a limit on how many times other threads can enter before it — no thread should starve.

These three requirements are precise and non-negotiable. A solution that provides mutual exclusion but allows starvation is incorrect. A solution that prevents deadlock but occasionally lets two threads into the critical section simultaneously is incorrect. Simple software-only approaches like Peterson's algorithm satisfy all three requirements but rely on assumptions about memory ordering that modern processors violate. This is why practical systems use hardware-supported atomic instructions (test-and-set, compare-and-swap) as building blocks for higher-level synchronization primitives like mutexes and semaphores, which you will study next.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Kernel Architecture and OS Structure → System Calls and User/Kernel Mode → Processes and the Process Control Block → Process Creation: fork() and exec() → Process Termination and Resource Cleanup → Process States and State Transitions → Threads and Concurrency → The Critical Section Problem and Race Conditions

Longest path: 99 steps · 363 total prerequisite topics

Prerequisites (3)

Leads To (5)