int ticket = 0; int serving = 0;
int myticket = ticket++; while (myticket != serving) { // wait } ... // critical section serving++;
int wantIn[2] = {false, false}; int turn = 0;
int me = 0; int you = 1 - me;
wantIn[me] = true; while (wantIn[you]) { if (turn != me) { wantIn[me] = false; while (turn != me) { // wait } wantIn[me] = true; } } ... // critical section turn = you; wantIn[me] = false;
Thread 1 Thread 2 A=1 B=1 print(B) print(A)