volatile registers (was: The world is not ready for 'volatile')

J.BEYER beyer at houxs.ATT.COM
Fri Jan 6 05:12:26 AEST 1989


In article <2337 at cuuxb.ATT.COM>, dlm at cuuxb.ATT.COM (Dennis L. Mumaugh) writes:
> In      article      <9316 at ihlpb.ATT.COM>       nevin1 at ihlpb.UUCP
> (55528-Liber,N.J.)  makes  some comments about volatile that seem
> to reflect many people's understanding.
> 
> The concept "volatile" should not occur in any general C program.
> This  feature  exists only for the stand alone, the real time and
> the kernel hacker.
>
I disagree with this statement.
Consider a program with a global variable that can be accessed by the
'main' program and a signal handler. On receipt of the signal, the
signal handler might change the value of the global variable. When the
signal handler returns, the 'main' program might observe the value of
the global variable. Were this variable not declared volatile, the main
program's code might be optimized so that the changed value would not
be detected. Were the main program be stuck in a 'busy-loop' waiting
for the global variable to change, and an optimizer (or clever compiler)
optimized the test outside the loop, the main program would never get out.
You may not care for this programming style, but it is legal and it is done.
 
-- 
Jean-David Beyer
A.T.&T., Holmdel, New Jersey, 07733
houxs!beyer



More information about the Comp.std.c mailing list