// Indicate the intent to enter the critical section
bIWantToEnter = true;
while (bHeWantsToEnter)
{
// If the other process indicates the same intent and
// it is not our turn, back off to give the other
// process a chance
if (iWhoseTurn != MY_TURN)
{
bIWantToEnter = false;
while (iWhoseTurn != MY_TURN) { }
bIWantToEnter = true;
}
}
// Code of critical section comes here
...
iWhoseTurn = HIS_TURN;
bIWantToEnter = false;