volatile
volatile tells the compiler that an object's value can change, or be observed, outside the abstract machine. Every read and write in the source must appear as a real memory access, in the order the thread wrote them relative to other volatiles.
Detailed Explanation
The optimizer's job is to delete work it can prove is useless. If you write x = 1; x = 2; and nothing in the function reads x in between, the first store can go. If you write while (ready == 0) {} and ready is an ordinary int, the compiler may load it once and loop forever on the copy in a register.
volatile int ready; closes that door. Each appearance of ready in the source is a trip to memory. A store is issued. A load is issued. The compiler will not cache the value across a sequence point, and it will not delete a store just because it cannot see a reader in this translation unit.
That is the entire guarantee. volatile is not a mutex. It is not atomic. It does not make a write from thread A visible to thread B in any particular order with respect to other memory. It does not prevent tearing of a 64-bit store on a 32-bit machine. Data races on volatile objects are still data races, except for the narrow case of sig_atomic_t and signal handlers.
The legitimate uses are few and specific.
Hardware registers. A memory-mapped status register can change when the device updates it. A command register must be written even if you write the same value twice. Those objects are volatile.
setjmp / longjmp. Locals that must keep a value across a longjmp need volatile, because the compiler may otherwise restore them from registers that longjmp does not know about.
Signal handlers. A flag the handler sets and main polls is volatile sig_atomic_t.
The illegitimate use is inter-thread communication. Use _Atomic, <stdatomic.h>, or a mutex. A volatile flag "to publish a buffer" is a bug that will pass testing and fail under a new compiler or a new CPU.
Code Examples
#include <stdio.h>
#include <setjmp.h>
static jmp_buf env;
int main(void)
{
volatile int step = 0; /* survives longjmp; a plain int might not */
if (setjmp(env) == 0) {
step = 1;
longjmp(env, 1);
}
printf("%d\n", step); /* prints: 1 */
return 0;
}#include <stdio.h>
#include <signal.h>
static volatile sig_atomic_t stop;
static void on_int(int sig)
{
(void)sig;
stop = 1; /* async-signal-safe assignment */
}
int main(void)
{
if (signal(SIGINT, on_int) == SIG_ERR) {
return 1;
}
while (stop == 0) { /* must re-read stop each iteration */
/* work */
break; /* demo: do not spin in the glossary example */
}
printf("%d\n", (int)stop); /* prints: 0 in this short run */
return 0;
}Common Gotchas
`volatile` is not a concurrency primitive
two threads sharing a `volatile int` without atomics or a lock have a data race. Use `_Atomic` or a mutex.
`volatile` does not make a wide store atomic
a `volatile uint64_t` on a 32-bit CPU can tear. Use `atomic_uint_least64_t`.
Only accesses through a `volatile`-qualified lvalue are protected
`int *p = (int *)&vreg;` then `*p` drops the qualifier and the optimizer is free again.
Compilers can still reorder `volatile` accesses against non-volatile ones
the "no reorder" rule is among volatiles, not a full memory barrier.