Content deleted Content added
m →External links: -ru |
→What can happen if volatile is not used?: Mention possibility of another thread and shared memory; expand a sentence |
||
Line 28:
*addr = 0;
while (
; /* do nothing, just loop */
}</nowiki></pre>
and the program will loop forever.
However, the address might represent a ___location that can be changed by other elements of the computer system. For example, it could be a [[hardware register]] of a device connected to the [[CPU]]. The value stored there could change at any time. Or, the variable may be modified by another [[thread (computer science)]] or [[process (computing)]] via [[shared memory]]. The above code would never detect such a change; without the volatile keyword, the compiler assumes the current program is the only part of the system that could cause the value to change. (This is by far the most common situation)
To prevent the compiler from modifying code in this way, the volatile keyword is used in the following manner: -
|