Well, to satisfy my own curiosity about modern compilers, `volatile', and memory barriers I ran a bunch of tests last night while I was asleep.

My testbed includes:

nine modern compilers.
compiled for 32 bit and 64 bit targets where possible.
compiled for "GNU/Linux" and "Windows" where possible.
compiler with and without optimizations where possible.
poor code written assuming `volatile' does provide some atomic and ordering guarantees beyond reality.
simple code written without `volatile' that uses locks with memory barriers.
simple code written with `volatile' that uses locks with memory barriers.
simple code written without `volatile' that uses locks that has no memory barrier.
simple code written with `volatile' that uses locks that has no memory barrier.
obfuscated code written without `volatile' that uses locks with memory barriers to try and force the compiler do something "kinky".
obfuscated code written with `volatile' that uses locks with memory barriers to try and force the compiler do something "kinky".
obfuscated code written without `volatile' that uses locks that has no memory barrier to try and force the compiler do something "kinky".
obfuscated code written with `volatile' that uses locks that has no memory barrier to try and force the compiler do something "kinky".

Unsurprisingly `volatile' didn't do anything it wasn't designed to do.
Unsurprisingly `volatile' didn't prevent the optimizer from doing something "kinky".
Unsurprisingly the lack of `volatile' didn't break code using locks with memory barriers.
Unsurprisingly the lack of `volatile' didn't break obfuscated code in that it was also broken with `volatile'.

Surprisingly, to me at least, only three tests presented a situation where `volatile' actually worked to prevent correct code using locks that has no memory barrier from breaking. (These tests were focused on whether or not `volatile' could prevent the compiler from caching a old value.) The majority of tests from this category were successful even without `volatile'.

With my own evidence bashing me over the head I can't help but change my suggestion. You should not reach for `volatile' by default where a variable may be mutated beyond the view of the compiler by the great "elsewhere" as I asserted. You should simply not bother with `volatile' unless you've managed to prove that the lack of `volatile' is significant. It isn't so much that it can't help in a few very specific situations; it absolutely can and does. It just happens that these cases are likely to be extremely rare so reaching for something that can cripple optimizations by default is severely flawed.

Soma