A deep dive into the `volatile` keyword in C/C++ and Java, explained through the lens of embedded systems programming. Covers why data can change unpredictably from external agents (other threads, OS, hardware registers), how `volatile` forces the compiler to perform actual memory reads/writes instead of optimizing them away, and the correct syntax for passing volatile pointers to functions. Also addresses the read-modify-write problem in microcontrollers, the difference between C and Java volatile semantics, and why volatile alone is insufficient for thread safety — recommending proper concurrency libraries and memory barriers instead. Includes a brief tour of CPU memory reordering and the happens-before relationship.
Table of contents
Volatility 101: Pay no attention to that man behind the curtain!Programming Language Support for VolatilityVolatility 304: Yoda, Memory Models, and the Posse in the BasementWrapupFurther reading in the basement33 Impressions