4 ms·
I've always hated the modern C++ atomic stuff. Atomicity is not a property of the thing being accessed, it is a property of the instructions performing the acce
by jcalvinowens 4y ago
I've always hated the modern C++ atomic stuff. Atomicity is not a property of the thing being accessed, it is a property of the instructions performing the access: trying to shoehorn that reality into an object-based model was wrong from the beginning.
A library of fence primitives is much easier to work with and understand, IMHO. [1] is the best example I know of. If you've never tried to write code that way, have a crack at it: you'll definitely learn something.
I think lots of programmers struggle to understand memory ordering because the C++ model makes it so much more difficult to understand than it really is. When you actually deal with the fences directly, everything makes a lot more sense.
Also, the word "atomic" is unfortunately really overloaded. Nearly all X86 instructions are "atomic" with aligned pointers, in the sense that the result of two racing MOV instructions will be one or the other value, not some corrupt combination of the two.
[1] https://www.kernel.org/doc/Documentation/memory-barriers.txt https://www.kernel.org/doc/Documentation/memory-barriers.txt
- gpderetta 4y agoI for one vastly prefer fenced operations as opposed to separate fences. The synchronizes-with model is much easier to understand than anything reordering based. Also it is nice that the typesystem helps making sure that atomic things are handled atomically. When it is not enough now there is atomic_ref.
- jcalvinowens 4y ago> The synchronizes-with model is much easier to understand than anything reordering based. Why do you think that?
- gpderetta 4y agoEmpirically it is much easier to reason about and be confident of the correctness of the result, at least to me and to many people I have discussed the matter with. I find it easier to reason in term of DAGs instead of all possible interleaved executions. The DAGs are stil dynamic and execution dependent, but you have to consider less combinations. One you know that some algorithm violates the happen before relation is easy to go back and find the reordering that would cause it.