C++ Reference ยท Keywords and Statements
The volatile qualifier
Reference for C++ volatile: why the compiler must not optimize away accesses, memory-mapped I/O and signal flags, and why it is not a substitute for atomics.
volatile is a type qualifier that tells the compiler an object may be changed, or observed, by something outside the program's normal flow: a hardware register, a memory-mapped device, a signal handler. Every read and write of a volatile object counts as an observable side effect, so the compiler may not remove, merge, cache in a register or invent such accesses.
What volatile does not do is just as important. It does not make operations atomic, it does not order accesses relative to other memory, and it does not synchronize threads. For sharing data between threads use std::atomic or a mutex.
volatile int *status = reinterpret_cast<volatile int*>(0x4000'1000); // device register volatile std::sig_atomic_t stop = 0; // signal flag
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
volatile type name; volatile type* p; // pointer to volatile type* volatile p; // volatile pointer type function() volatile; // volatile member function const volatile type name; // both qualifiers (a read-only hardware status register)
| Position | Meaning |
|---|---|
volatile T x |
Every access to x is an observable side effect and happens in program order relative to other volatile accesses. |
volatile T* |
Accesses through the pointer are volatile. |
T* volatile |
The pointer itself is volatile. |
const volatile T |
Not writable by the program but may change by itself. |
Usage notes
volatilesuits memory-mapped I/O, variables touched bysetjmp/longjmpandstd::sig_atomic_tflags written by signal handlers. In application code it is rare.- It is not a threading tool. Concurrent unsynchronized access to a
volatile intis still a data race and undefined behavior. Java'svolatilehas different semantics; do not transfer the meaning. - Compound operations on
volatileobjects are not atomic:++vreads, adds and writes as separate accesses. C++20 deprecated++,--and compound assignment on volatile operands for exactly this reason; writev = v + 1if you mean separate read and write. - The compiler may not reorder volatile accesses relative to each other, but it may still reorder them against normal accesses, and the CPU can reorder memory operations too.
- A
volatileobject of class type needsvolatilemember functions to be used; most standard library types do not provide them, sovolatile std::vector<int>is almost useless. - Deprecated in C++20: volatile-qualified parameter and return types (
void f(volatile int)), and structured bindings to a volatile. - Do not use
volatileto stop the compiler from optimizing benchmarks away; use dedicated "do not optimize" helpers or write results to an output. - Casting away
volatileand accessing a volatile object through a non-volatile glvalue is undefined behavior.
Related
- const
- Atomics
- Data races
- Tutorial: Atomics, Signal handling