2 ms·
Load/acquire and store/release are sufficient pretty much when you're implementing a mutex-style operations, so you have a lock on a single value guarding vanil
by jcranmer 8y ago
Load/acquire and store/release are sufficient pretty much when you're implementing a mutex-style operations, so you have a lock on a single value guarding vanilla memory operations that are unused outside of the mutexes. In pretty much any other scenario, (including, notably, most lock-free algorithms), it's best to start with sequential consistency and don't try to relax the memory unless you can prove correctness.
The actual bug in question used relaxed memory ordering, and the fix was to use acquire/release. Relaxed memory ordering really should be reserved for scenarios where you need atomicity but absolutely no synchronization (statistics counters are the only case that readily comes to mind).
- AstralStorm 8y agoNote that C++11 std::mutex and C11 mtx_t require a single total order of operations on the mutex, which means acquire plus release pairs equivalent on lock/unlock. That is actually weaker than acq rel write. POSIX requires memory barrier in addition to that on the pthread pair, making it get more different, and both major C++ library implementations defer to pthreads.