In addition to problems due to the non-atomic nature of the updating of variables, another problem can also occur. Let’s take a look at the following code:
Even if x is declared global, a smart compiler with optimization turned on will “optimize” the loop out because it is not possible to enter the loop. However, what if the following code is in an ISR or signal handler?
This means that optimizing the loop out is not the right thing to do! An ISR or signal handler can change the value of x asynchronously.