DEV Community

Cover image for Peterson's Algorithm Explained: Software Mutual Exclusion and Modern Memory Barriers
Binary Dose
Binary Dose

Posted on Originally published at binarydose.in

Peterson's Algorithm Explained: Software Mutual Exclusion and Modern Memory Barriers

The Critical Section Problem

In concurrent programming, whenever multiple threads share mutable state without synchronization, race conditions arise.

Any valid solution to the Critical Section problem must satisfy three fundamental criteria:

  1. Mutual Exclusion: If process P_i is executing in its critical section, no other processes can be executing in their critical sections.
  2. Progress: If no process is executing in its critical section and some processes wish to enter, only those processes not executing in their remainder sections can participate in deciding who enters next.
  3. Bounded Waiting: There must be a bound on the number of times other processes are allowed to enter their critical sections after a process has made a request to enter and before that request is granted.

Peterson's Algorithm (1981)

Gary Peterson published an ingenious, classic software-based solution for two processes (P₀ and P₁) that requires zero specialized hardware atomic instructions.

It relies on two shared variables:

bool flag[2] = {false, false}; // Expresses intent to enter
int turn = 0;                  // Expresses polite priority
Enter fullscreen mode Exit fullscreen mode

The Process Structure for Thread i (with peer j = 1 - i):

do {
    flag[i] = true;
    turn = j;
    while (flag[j] && turn == j) {
        // Busy wait (spin)
    }

    // --- CRITICAL SECTION ---

    flag[i] = false;

    // --- REMAINDER SECTION ---
} while (true);
Enter fullscreen mode Exit fullscreen mode

The Mathematical Proof of Mutual Exclusion

To prove mutual exclusion holds, we use proof by contradiction:

  1. Suppose both P₀ and P₁ are simultaneously executing in their critical sections.
  2. For P₀ to exit the while loop, either flag[1] == false or turn == 0.
  3. For P₁ to exit the while loop, either flag[0] == false or turn == 1.
  4. Since both are in their critical sections, both flag[0] == true and flag[1] == true.
  5. Therefore, we must have turn == 0 AND turn == 1 simultaneously.
  6. Contradiction: turn is a scalar memory location in shared RAM; it can only hold one integer value at a time. The last write to turn overrides the previous write.

Thus, mutual exclusion is mathematically guaranteed.


Why Peterson's Algorithm Breaks on Modern CPUs

Peterson developed his proof under the assumption of Sequential Consistency—that memory reads and writes occur in the exact chronological order specified by the source code.

Modern hardware, however, relies heavily on:

  • Compiler Instruction Reordering: The optimizer may reorder independent writes.
  • Out-of-Order CPU Execution: Store buffers on x86 and weak memory models on ARM can make the store to turn visible to other cores before the store to flag[i].

If turn = j is reordered before flag[i] = true:

  1. P₀ sets turn = 1.
  2. P₁ sets turn = 0.
  3. P₁ sets flag[1] = true.
  4. P₁ checks: flag[0] is still false implies P₁ enters!
  5. P₀ sets flag[0] = true.
  6. P₀ checks: turn == 0 is false (it's 0, but P₀ checks turn == 1) implies P₀ enters!
  7. MUTUAL EXCLUSION FAILS!

To run Peterson's algorithm correctly on modern multicore hardware, explicit Memory Barriers (std::atomic_thread_fence(std::memory_order_seq_cst)) are strictly required.

Explore our full Operating Systems concurrency curriculum on Binary Dose.

Top comments (0)