by Matheus28 · 11y ago · 0 comments
Suggest either std::atomic or intrinsics, but don't give false information in a tutorial.
Video: https://www.youtube.com/watch?v=X1T3IQ4N-3g&feature=youtu.be...
PDF: https://github.com/CppCon/CppCon2014/tree/master/Presentatio...
Don't use it as a synchronization primitive, that's not what it's meant to do. You only should use it:
a. If you're reading from HW.
b. If you want to use it like a const marker for member functions.
1. Reading or writing shared memory
2. Values which may be modified in signal handlers
3. Implementing atomic types (e.g. boost::atomic)
4. RCU (e.g. ACCESS_ONCE in Linux kernel)
Really you should be using the appropriate platform memory and execution barriers in place of volatile 99% of the time.
Yeah I was bit by that when writing a shared memory low latency library. When compiled under debug it worked, in release mode values were not being written to the shared memory area. Added some "volatile"-s in there and it worked.
volatile the keyword was not written with the intent of being a memory barrier (edit: or atomicity for that matter) in mind.
In practice, I've created unit tests to catch and fix issues on mobile (specifically iOS - ARM arch, under both GCC and later Clang compilers IIRC) with our "lock free" circular buffer.
It was indeed lock free. And also just plain broken in multi-core scenarios.