In the [[computerC (programming language)|C]] [[programming language]], a [[variable]] or object declared with the '''volatile''' [[Keywordkeyword (computer programming)|keyword]] may be modified externally from the declaring object. For example, a variable that might be concurrently modified by multiple [[Threadthread (computer science)|threadsthread]]s should be declared volatile. Variables declared to be volatile will not be [[compiler optimization|optimized]] by the [[compiler]] because the compiler must assume that their valuevalues can change at any time. Note that operations on a volatile variable are still ''not'' guaranteed to be [[Atomicatomic operation|atomic]].
==What can happen if volatile is not used?==
The following piece of [[C (programming language)|C]] [[source code]] demonstrates the use of the <code>volatile</code> keyword.
<code>
<pre><nowiki>
static int [[foo]];
int *addr;
addr = 100;
foo = 0;
*addr while (foo != 0;255)
}
</code>
In this example, the code sets the value stored at [[memory address|___location]] 100 in the computer system<code>foo</code> to 0. It then starts to [[polling (computer science)|poll ]] that thevalue addressrepeatedly until it changes to 255. ▼
}</nowiki></pre>
▲In this example, the code sets the value stored at [[memory address|___location]] 100 in the computer system to 0. It then starts to poll the address until it changes to 255.
An [[Compileroptimizing optimization|optimizingcompiler]] compiler will assumenotice that no other code willcan possibly change the value stored in ___location 100<code>foo</code>, and therefore assume that it will remain equal to 0 at all times. The compiler will then replace the function body with an [[program loop|whileinfinite loop]] with something, similar to this: -
<code>
<pre><nowiki>
void foobar_optimized(void)
{
int *addrfoo = 0;
addr =while 100;(TRUE)
continue;
}
*addr = 0;
</code>
However, the address<code>foo</code> 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)|thread]] or [[process (computing)|process]] via [[shared memory]] . It could even be a [[hardware register]] of a device connected to the [[CPU]]. The value stored there could change at any time. The above code would never detect such a change; without the <code>volatile </code> 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 .) ▼
while (TRUE)
; /* do nothing, just loop */
}</nowiki></pre>
To prevent the compiler from modifying code in this way, the <code>volatile </code> keyword is used in the following manner: ▼
and the program will loop forever.
<code>
▲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)|thread]] or [[process (computing)|process]] 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)
static '''volatile''' int foo;
▲To prevent the compiler from modifying code in this way, the volatile keyword is used in the following manner:
void bar(void)
<pre><nowiki>
void foo(void) = 0;
▲ while ( *addrfoo != 255)
volatile int *addr continue;
}
</code>
With this modification the codeloop condition will remainnot asbe itoptimized isaway, and the CPU will detect the change when it occurs. ▼
/*...same as before */
}</nowiki></pre>
For an example of the use of <code>volatile</code> in context, see [[Busy waiting]].
▲With this modification the code will remain as it is and the CPU will detect the change when it occurs.
==External links==
[[Category:Programming constructs]]
[[Category:C programming language]]
|