I have seen this C code showing an implentation of Peterson's Critical Section algorithm. It is obviously skeletal and hardwired for two threads but the logic is supposed to be correct in detail.

[![Peterson's algorithm in pseudocode](https://i.stack.imgur.com/0eRVx.png)](https://i.stack.imgur.com/0eRVx.png)

Despite reading and talking I remain with a grain of doubt about the following line (and the similar line for Thread B):

> 
> `while (turn == 1 && flag1) skip;
> 
> 
> 

When it is compiled, the while clause will generate multiple instructions which it seems to me can lead to a race condition in a pre-emptive scheduling model.

While I trust the proofs etc. I have not seen a good way to refute my concern.

(I know this is an oldie but goodie so feel free to respond with a link.)