volatile in C is basically an anachronism designed for a completely different scenario and NOT useful for multi-threaded programming. For one reason or another, even the latest C ++ standard could not give it more meaning, but had to come up with a new keyword.
Remember that this means that volatile , as defined by the C standard, is useless, compilers can give you additional guarantees (I know what MS VC does, and I think it gives basically the same guarantees as volatile in java) , but that means the program is blocked by this compiler. Most compilers also have some options for inserting memory barriers, which are slightly more explicit, but again not portable as such.
In practice, you are probably best using a higher-level thread library that offers the right tools for the job. For instance. POSIX gives you low-level afaik memory barriers.
On a C ++ site, better with 0x11 - the standard offers std::atomic_thread_fence and atomic variables. Note that the C ++ 0x11 memory model is NOT identical to java, so you need to be careful when porting.
source share