r/cpp • u/Both_Helicopter_1834 • Feb 17 '26
Pushback on the C++ memory ordering model
How many people are there in the world who feel they have a thorough understanding of the Standard C++ memory ordering model? It seems both unnecessarily elaborate, but also too vague. It seems much more straightforward to specify that memory write events are caused by one thread, but occur in all threads, just not necessarily in the same order. Fenced atomic writes and standalone fences in the causing thread restrict the ordering of the memory writes it causes in other threads. I took a stab at trying to write something up: https://wkaras.github.io/CppMemOrder.html . Feedback welcome.
8
u/Expert-Map-1126 vcpkg maintainer BillyONeal Feb 17 '26 edited Feb 17 '26
I believe that one of the reasons that the standard memory model is more complex is that they are explicitly trying to allow machines that violate this requirement. (And, in fact, all weaker than
seq_cstmodels produce inconsistent operation orderings.)Couple of bits you might find interesting:
3
u/Plastic_Fig9225 Feb 17 '26
What element of C++ code constitutes a "write event" that implies (eventual) visibility in all threads?
41
u/ReDucTor Game Developer | quiz.cpp-perf.com Feb 17 '26
The memory model isn't super hard, designing concurrent algorithms and data structures is the hard part.
3
u/osmin_og Feb 17 '26
I was asked about memory ordering during interviews more than once. It is not a hard topic.
12
u/bjorn-reese Feb 17 '26
The C++ memory order covers three intertwined topics.
First, atomic reads and writes must be untorn. Reading a variable while it is modified by another thread must not result in a partially updated modified value. For example, this is the only guarantee that relaxed memory order gives.
Second, a sequence of instructions may be reordered by the compiler or the CPU (out-of-order execution.) Memory order restricts this reordering. For example, instructions cannot be moved before an acquire or after a release.
Third, multiple cores that share the memory system notifies each other about changes to cached values. This happens periodically (cache coherence), but memory order can trigger notifications immediately. For example, acquire asks other cores for their changes, and release notifies other cores about local changes.
2
u/IcyWindows Feb 17 '26
Unless I need my code to build with many different compilers, I only really need to understand the one I actually use.
The standard is great to understand, but it's such a high bar that you really don't need with nearly any other language as they only have one tool chain.
4
u/Wooden-Engineer-8098 Feb 18 '26
people often think something is overcomplicated when they don't understand the problem that something tries to solve
1
u/holyblackcat Feb 17 '26
Something tells me there are very few people qualified to tell if this is equivalent to what we have now (or at least to what compilers/CPUs are doing), I'm definitely not one of them. :P
1
u/Successful_Yam_9023 Feb 18 '26
While I appreciate the effort, I don't really understand this proposal either, as far as I see it it still leaves the whole situation harder to reason about than plain old assembly.
Also what do you mean by stores "failing"?
0
u/[deleted] Feb 17 '26
[deleted] — view deleted comment