Skip to content
Tutorials Center
My learning

Topics

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.

C++
volatile int *status = reinterpret_cast<volatile int*>(0x4000'1000);   // device register
volatile std::sig_atomic_t stop = 0;                                    // signal flag

Try it

C++ Accesses that must not be optimized away

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.

C++ A signal flag and why volatile is not for threads

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

C++
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

  • volatile suits memory-mapped I/O, variables touched by setjmp/longjmp and std::sig_atomic_t flags written by signal handlers. In application code it is rare.
  • It is not a threading tool. Concurrent unsynchronized access to a volatile int is still a data race and undefined behavior. Java's volatile has different semantics; do not transfer the meaning.
  • Compound operations on volatile objects are not atomic: ++v reads, adds and writes as separate accesses. C++20 deprecated ++, -- and compound assignment on volatile operands for exactly this reason; write v = v + 1 if 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 volatile object of class type needs volatile member functions to be used; most standard library types do not provide them, so volatile 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 volatile to stop the compiler from optimizing benchmarks away; use dedicated "do not optimize" helpers or write results to an output.
  • Casting away volatile and accessing a volatile object through a non-volatile glvalue is undefined behavior.