In 2006 I was inspired by an (2001) Dr. Dobb's article by Andrei Alexandrescu how to use volatile as a type modifier, similar to const. Now I have grown in my understanding of volatile and although it can look attractive for threaded code you should think twice about why you want to use it.
Let's get some facts straight
What is volatile good for?
The volatile keyword is not a threading or synchronization primitive in portable C or C++. It was mainly intended to
What can volatile be used for, even if it's not the "intention" of it?
It is better to use memory barriers and locks to guarantee correctness when reordering (CPU, Compiler, Memory)
That said, it is also not uncommon to use volatile for lock-free ring buffers used by network devices or other "input/output device" where the device will change the pointers to indicate what has been processed. I have also seen this in use in lock-free ring buffers that act as the message queue between interrupt driven firmware drivers and application layer.
A simplified example of this is my own example of a Lock-Free Single Producer Single Consumer queue. You can find it here or at CodeProject.
However, even this is actually a little like Russian Roulette. Even if the synchronization is not guaranteed by the volatile itself. The volatile only guarantees that volatile won't be reordered in relation to other volatile (ref here). So why is it Russian Roulette? I think one of Herb Sutters arguments in the comp.lang.c++.moderated entry "Am I or Alexandrescu wrong about singletons?" says it all:
Please remember this: Standard ISO C/C++ volatile is useless for multithreaded programming. No argument otherwise holds water; at best the code may appear to work on some compilers/platforms
So what (else?) can we use volatile for? Well, Andrei Alexandrescu explains some of it in his article. I have summarized some of his points below at
"So what about Andrei's article about Volatile and LockingPtr?". I've also added some very strong complaints that this is absolutely wrong from the C++ standards view (even if it seemingly seems to work).
What is volatile NOT good for?
- Google group discussion. comp.lang.c++.moderated. People from the C++ committee participated in the discussion and some VERY experienced software gurus. Originally about singletons but turned quickly
towards volatile: "Am I or Alexandrescu wrong about
- Intel blog with VERY interesting discussion "Volatile Almost useless
- Comp.programming.threads regarding
Andrei Alexandrescu's article
"Does anyone think 'volatile' is a platform-independent way to make variable access thread safe?"
- And of course Andrei's article
"volatile - Multithreaded Programmer's Best Friend"
- My article on code project. "Lock-Free Single-Producer - Single Consumer
- Herb Sutter "Volatile Vs Volatile"
- Scott Meyers and Andrei Alexandrescu (now he warns for using volatile,
but not a warning against usage as he proposed before) "C++ and
the Perils of Double-Checked Locking"
- Very good and informative blog entry by John Regehr. The best source so far? that explains volatile, how it works and what to expect when using it. It very strongly suggests to NOT use it unless for its right purpose: Nine ways to break your systems code using volatile