3 ms·
Why do people use C11’s atomic operations without a specified memory order? The qsbr.c doesn’t appear to explain why it has chosen the default order (sequential
by cormacrelf 4y ago
Why do people use C11’s atomic operations without a specified memory order? The qsbr.c doesn’t appear to explain why it has chosen the default order (sequentially consistent) for all of its operations. I don’t see any arguments about the release and acquire ordering that SeqCst implies or why sequential consistency is needed. The implicit seqcst variants of every atomic operation should never have been part of the API IMO. At least in the Rust code I read, where people are forced to confront the choice and specify one, there is very often an attempt to justify the use of particular orderings. When people aren’t forced and don’t comment on it, I can’t tell the difference between someone who really knows their stuff and eg knows it doesn’t matter for correctness or performance, and someone using the “use seqcst it’s never wrong” strategy which is of course not true.
From Mara Bos’ new book (https://marabos.nl/atomics/memory-ordering.html#common-misconceptions https://marabos.nl/atomics/memory-ordering.html#common-misco...):
> More importantly, when reading code, SeqCst basically tells the reader: "this operation depends on the total order of every single SeqCst operation in the program," which is an incredibly far-reaching claim. The same code would likely be easier to review and verify if it used weaker memory ordering instead, if possible. For example, Release effectively tells the reader: "this relates to an acquire operation on the same variable," which involves far fewer considerations when forming an understanding of the code.
> It is advisable to see SeqCst as a warning sign. Seeing it in the wild often means that either something complicated is going on, or simply that the author did not take the time to analyze their memory ordering related assumptions, both of which are reasons for extra scrutiny.
When SeqCst is used explicitly, then it deserves scrutiny for either of those reasons, but when someone uses SeqCst implicitly, all I get is the latter.
That said, this is a very good post. It very clearly laid out the way quiescent states are useful and how libuv gives you one, and did so very efficiently, without any waffling on.
- fanf2 4y agoGood comment: you are exactly right that I have handwaved the ordering dependencies, and I need to go back and review them properly. Mara Bos’s book arrived through my letter box earlier this week, so thanks for pointing out that it has some good guidance. I have got the QSBR code working end-to-end now, https://dotat.at/@/2023-01-24-qp-bench.html https://dotat.at/@/2023-01-24-qp-bench.html and there are a few areas (including these atomic ops) that need cleanup before it is properly ready to merge.
- cormacrelf 4y agoI’m glad to hear it. I’ll be watching out for updates, it’s very interesting stuff!