First, the way this algorithm is written, in particular using array references
flag[j], instead of two single variables, e.g.
flag_p1, suggests to the reader that it would scale easily to three or more threads. However, that is simply not the case. The busy wait loop test condition has to enlarge to accommodate each new thread, or the data structures needed and tests change substantially) to generalize for N threads.
So, in the context of just this two process version as was originally developed: the actual value written to
turn is unimportant as long as:
- for each thread, the same value is written as is tested for equality in the busy wait (or, the opposite value is tested for inequality), and,
- the other thread uses the opposite constant value
The point is that either the other thread is no longer interested in the protected resource (
flag[j] fails), or, the other thread is interested but is still busy waiting (
flag[j] is true, but the value written is now the other value).
These two tests together cover not only interest in accessing the protected resource, but problematic issues like race condition that would otherwise occur if the
turn memory location weren't also involved. In short, it takes three words of state to accomplish this algorithm (and each word actually needs only 1 bit!).
The specific values used for the
turn variable are not important, and do not have to be
i for Pi and
j for Pj; they just need to be different values used in the two different processes.
You could just as easily write
turn if you like, e.g. for Pi
flag[i] = true; // register interest
turn = i; // write to the turn variable to detect busy condition
while (flag[j] && turn == i); // test (lack of interest or busy wait) by Pj
as long as Pj is similarly adjusted.
Or we could dumb it down to the essence without the (arguably erroneous) allusion to the generalized N-thread version that uses arrays, as there are only two processes supported in the original algorithm. Here for P0
flag_p0 = true; // register interest
busy = 100; // write to the turn variable to detect busy condition
while (flag_p1 && busy == 100); // test (lack of interest or busy wait) by p1
And for P1
flag_p1 = true; // register interest
busy = 200; // write to the turn variable to detect busy condition
while (flag_p0 && busy == 200); // test (lack of interest or busy wait) by p0