- The
volatile
keyword tells the compiler that a variable's value may change unexpectedly, outside the program's control (e.g., hardware, interrupts, etc.). - It prevents the compiler from optimizing reads and writes to the variable, ensuring that every read/write of a variable occurs exactly as written in the code.
- It is used when variables might be modified by hardware, interrupts, or external threads.
volatile Type var_name;
volatile Type* var_ptr;
- Embedded systems:
- Hardware registers, memory-mapped I/O, and flags.
- Interrupt service routines (ISRs):
- Variables modified by ISRs to ensure the main program does not cache them.
- Multi-threading (discouraged):
- Shared variables accessed by multiple threads (use
std::atomic
instead for thread synchronization).
- Shared variables accessed by multiple threads (use
- No atomicity:
- Does not guarantee atomic access or synchronization between threads.
- No memory ordering:
volatile
does not enforce memory ordering or prevent reordering of instructions.
- Not a synchronization mechanism:
volatile
should not be used for thread synchronization.- Use
std::atomic
or other synchronization techniques instead.
- Does not ensure thread safety:
- Used for hardware interaction or volatile memory locations, not for controlling access to shared variables between threads.