Talk:Optimizing C++/Writing efficient code/Performance worsening features

Volatile and Threading

Read http://www.ddj.com/architect/184405772 and http://kerneltrap.org/mailarchive/linux-kernel/2007/5/8/88107

Volatile is defined in terms of C but not defined for the machine itself. Therefore the CPU, the memory cache and the memory bus may all reorder operations on "volatile" values. This will not be thread-safe.

The Intel x86 architecture is somewhat forgiving because it usually does memory operations in order. However, "usually" is not "always", especially with deeper pipelines and more speculative execution. On RISC or EPIC CPU architectures code that uses volatile for locking will be totally screwed.