Does volatile make a variable access atomic?
0 upvotes
Practice with AISoon
No. volatile only prevents the compiler from optimizing away or reordering accesses. It says nothing about atomicity at the hardware level. On a 32-bit ARM Cortex-M, a volatile uint32_t read is atomic because the bus performs a single 32-bit load. But a volatile uint64_t requires two separate 32-bit loads — an interrupt between them can corrupt the value.
For true atomicity, you need either: (1) disabling interrupts around the access, (2) using architecture-specific atomic instructions (LDREX/STREX on ARM), or (3) using a mutex in an RTOS context. volatile is necessary but not sufficient for shared-variable safety.
Source: C / C++ Concepts Q&A
