What is the purpose of the volatile keyword in C++? Does it provide any thread-safety guarantees?

Last updated 21 June 2026

  • volatile tells the compiler a variable may change outside the program's control, so every read/write must actually happen (no register caching or elision). Legit uses: memory-mapped hardware registers and signal-handler flags (with sig_atomic_t).

  • It gives no thread-safety: no atomicity (read-modify-write is still non-atomic) and no cross-thread memory ordering or fences.

  • Use std::atomic for concurrency. Unlike Java/C# volatile, the C++ keyword implies no memory ordering (a common interview trap).

  • https://www.interviewbit.com/cpp-interview-questions/
  • https://www.geeksforgeeks.org/cpp/cpp-interview-questions/
  • https://dev.to/pauljlucas/advanced-thread-safety-in-c-3ap5
  • https://www.scribd.com/document/944321897/C-Interview-Questions
  • https://blog.mettl.com/cpp-interview-question/
All C++ interview questions