P1 running... x += 4; ==> LOAD x ADD #4 P2: x++; ==> LOAD x ADD #1 STORE x STORE x ------------------------------------------- P1 running... x += 4; ==> LOAD x ADD #4 P2: x++; ==> WAIT/ BLOCK STORE x P2: x++; ==> LOAD x ADD #1 STORE x =========================================== /* shared between P1 and P2 */ int x = 5; int lock = false; /* P1 */ while ( 1 ) { executeNonCriticalSection(); while ( lock == true ) { /* TEST */ /* wait ... */ } ****PC*************** lock = true; /* SET */ executeCriticalSection(); lock = false; } /* P2 */ while ( 1 ) { executeNonCriticalSection(); while ( lock == true ) { /* wait ... */ } ************PC***************** lock = true; executeCriticalSection(); lock = false; } =========================================== /* shared between P1 and P2 */ int x = 5; int needLock1 = 0; int needLock2 = 0; int turn = 1; /* P1 */ /* P1 */ while ( 1 ) { executeNonCriticalSection(); needLock1 = 1; turn = 2; while ( turn == 2 && needLock2 == 1 ) { /* wait ... */ } executeCriticalSection(); /* access x */ needLock1 = 0; } /* P2 */ while ( 1 ) { executeNonCriticalSection(); needLock2 = 1; turn = 1; while ( turn == 1 && needLock1 == 1 ) { /* wait ... */ } executeCriticalSection(); /* access x */ needLock2 = 0; }