Peterson's algorithm is a correct solution to the critical section problem and it is simpler than Dekker's algorithm. It requires both an array of boolean values and an integer variable:
var | flag: array [0..1] of boolean; |
turn: 0..1; |
The array elements are initialized to false and turn is either 0 or 1.
repeat         flag[i] := true;         turn := j;         while (flag[j] and turn = j) do no-op;                 critical section         flag[i] := false;                 remainder section until false; |
This algorithm guarantees the mutual exclusion, progress, and fairness requirements.