r/cpp • u/NekrozQliphort • Aug 28 '25
Explaining the Need for Strongly Happens Before in C++
https://nekrozqliphort.github.io/posts/happens-b4/I was digging into atomics when I came across strongly happens before. I was curious why it was needed, so I looked into the history and some of the issues that led to its proposal. What started as a planned study group talk didn’t pan out, so I turned it into a blog post instead.
Would love to hear feedback on the write-up!
62
Upvotes
29
u/aruisdante Aug 28 '25 edited Aug 28 '25
Overall this is a nice write up, partially with all the graphs. The one thing it’s missing, that I feel like a lot of discussions on atomics miss, is some kind of grounding in a practical problem that is solved by this knowledge. I feel like that would really help drive home the context more than abstract “a bunch of reading and writing to ints, what set of values do they see?” would despite that being the form that most writings on atomics wind up using.
48
u/ald_loop Aug 28 '25
not going to lie. If my job ever requires me to write multithreaded code like this and understand how these atomics are going to behave based off all these memory orderings, just fire me on the spot.
8
u/joz12345 Aug 28 '25
Theres actually a flaw in this wording in the standard + it's future is uncertain. It prohibits the established x86 implementation and it's currently pretty much ignored by compiler implementers:
https://cplusplus.github.io/LWG/lwg-active.html#3941
The original paper that the standard is based on defines this acyclic relation using mathematical notation rather than words, and captures more nuance.
The current wording with strongly-happens-before etc tried and failed to simplify it whilst putting it into words. They did manage to address the issues with Power architecture but broke x86 by strengthening seq_cst past what can be efficiently implemented.
This flaw can only happen when you mix seq_cst loads and non seq_cst stores within a single atomic, but it was explicitly mentioned in the academic paper as a complication of x86, so it's weird that the standard got it wrong.
Honestly it's a bit disheartening knowing the standardisation process can be this inefficient - that there was apparently no review from the original paper authors or any of their academic peers ahead of standardisation, and that we will likely have this complex but ultimately incorrect wording in the standard at least until c++29.
6
u/simpl3t0n Aug 28 '25
Looks interesting. I haven't read it yet, but if it's your own content (as opposed to AI), I applaud your efforts to bring the forbidden knowledge to the masses.
Separately, the fact that there's a cottage industry of explainers, points at the poor pedagogical skills on the part of the spec authors. The authors, one would think, are extraordinarily smart and know exactly what the problem is. Yet, somehow, whatever official documents/specs they produce, ends up laden with jargon and indigestible to the rest of us. How is that possible? Is simple, concise writing, with examples, that hard? Elsewhere, there's a term called The Monadic Curse: after when you understood the problem, you lose the ability to explain it to others.
I've been interested in this topic forever. I've read through a fair amount of literature, papers, blog posts, and videos. Still, I don't think I was able to build a mental model of the underlying problem. Nor do I think I can explain to someone what it is. At times, I feel I understood it; but the next article I read leaves me a deer under headlamps.
Maybe it's that I don't have enough brain cells.
3
u/[deleted] Aug 28 '25 edited Aug 28 '25
Good write-up. Another thing that bothers me is that there seems to be a defect in the new definition of strongly-happens-before in P0668R5: it doesn't include the
release-acquireedge. The standard library wording usually uses strongly-happens-before to express synchronizes-with (whereacquire/releaseis sufficient for the implementation). See this SO question for more details. P0668R5 states:But this is not the case, as mentioned above. I would love to hear more discussion on this matter.