Content deleted Content added
→In C and C++: For the relevance of this note, see https://stackoverflow.com/questions/38243501/does-accessing-a-declared-non-volatile-object-through-a-volatile-reference-point |
Mx. Granger (talk | contribs) +wikilink |
||
Line 1:
{{Short description|A keyword used in some programming languages to tag variables}}
{{Lowercase title}}
In [[computer programming]], particularly in the [[C (programming language)|C]], [[C++]], [[C Sharp (programming language)|C#]], and [[Java (programming language)|Java]] [[programming language]]s, the '''volatile''' [[keyword (computer programming)|keyword]] indicates that a [[Value (computer science)|value]] may change between different accesses, even if it does not appear to be modified. This keyword prevents an [[optimizing compiler]] from optimizing away subsequent reads or writes and thus incorrectly reusing a stale value or omitting writes. Volatile values primarily arise in hardware access ([[memory-mapped I/O]]), where reading from or writing to memory is used to communicate with [[peripheral device]]s, and in [[thread (computing)|threading]], where a different thread may have modified a value.
Line 13 ⟶ 12:
*allow uses of <code>sig_atomic_t</code> variables in signal handlers.
While intended by both C and C++, the C standards fail to express that the <code>volatile</code> semantics refer to the lvalue, not the referenced object. The respective defect report ''DR 476'' (to C11) is still under review with [[C17 (C standard revision)|C17]].<ref>[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2244.htm ''Clarification Request Summary for C11.''] Version 1.13, October 2017.</ref>
Operations on <code>volatile</code> variables are not [[atomic operation|atomic]], nor do they establish a proper happens-before relationship for threading. This is specified in the relevant standards (C, C++, [[POSIX]], WIN32),<ref name="auto"/> and volatile variables are not threadsafe in the vast majority of current implementations. Thus, the usage of <code>volatile</code> keyword as a portable synchronization mechanism is discouraged by many C/C++ groups.<ref>{{cite web |title=Volatile Keyword In Visual C++|url=http://msdn2.microsoft.com/en-us/library/12a04hfd.aspx|work=Microsoft MSDN}}</ref><ref>{{cite web |title=Linux Kernel Documentation – Why the "volatile" type class should not be used|url= https://www.kernel.org/doc/html/latest/process/volatile-considered-harmful.html|work=kernel.org}}</ref><ref>{{cite web |title=C++ and the Perils of Double-Checked Locking|url=http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf|work=DDJ|year=2004|author1=Scott Meyers |author2= Andrei Alexandrescu}}</ref>
|