This has been bothering me for a while, but to take a recent example, a question like What are the optimal std::memory_orders for this scenario of a coroutine waiting on an event? is about what std::memory_order
should be used with the atomic load, store, and RMW operations. But despite this, the question got posted with the memory-barriers tag because memory-order is currently a synonym for memory-barriers.
It doesn't involve std::atomic_thread_fence
, and if compiled for AArch64, won't need any barrier instructions instructions like dmb ish
, just ordered loads and stores with std::memory_order_acquire
and std::memory_order_release
ordering. Even std::memory_order_seq_cst
doesn't need separate barriers on AArch64, as ldar
loads can't reorder with later stlr
stores. Similarly for x86, if the pure loads and stores can all be weakened to acquire
and release
, so at most the RMWs are still seq_cst
, then no separate barrier instructions are needed to implement the C++. (x86 does need a barrier to do a seq_cst store, or do them with xchg
instead of mov+mfence).
And more importantly, whether a compiler needs to use any barrier instructions to implement the C++ semantics in the asm is just an implementation detail, quite separate from the correctness of the memory-order specified for operations. foo.load(std::memory_order_acquire)
is not a memory barrier, it just has one-way ordering. If fences like std::atomic_thread_fence(acquire)
were that weak, they wouldn't work. (Which is why they are in fact 2-way barriers).
When reasoning about correctness with different memory orders on your atomic operations in portable C++, you should be thinking in terms of operations, not in terms of how they compile to 2-way barriers on some ISAs like PowerPC or ARM before ARMv8. (Or if the argument is that "barrier" can describe the 1-way ordering of an atomic operation, then I don't like that confusing naming.)
So perhaps we should un-synonymize (https://stackoverflow.com/tags/memory-barriers/synonyms) memory-order and memory-ordering from memory-barriers + memory-fences.
Or better, reverse things so memory-order is the primary tag, i.e. make memory-barriers and memory-fences synonyms of memory-order.
Most of the time when I find myself adding one of those tags to a post, or see a new post with that tag, the right name is memory-order.
Using memory barriers/fences is one way to achieve ordering, so memory-order is the more general concept we're collecting together, currently under the memory-barriers tag name. I think the best end result would be to have all 4 of those tags still be synonyms, but of memory-order. (If that's easily possible.)
In case anyone's looking at similarly named tags:
barrier is about a separate concept: a thread-synchronization building block where threads that call
barrier(foo)
sleep until all threads have calledbarrier(foo)
, then they all wake up. This is unrelated to memory ordering and fences.relaxed-atomics should maybe be a synonym of [tag:memory_order] or deleted. The default is
std::memory_order_seq_cst
, so any weaker order requires an explicit memory_order parameter. And almost any question about what std::memory_order to use is at least considering the possibility of using weaker orders.OTOH, the memory-order tag also includes things like language memory model rules, and some like Java don't allow memory orders weaker than sequential consistency. So use of that tag doesn't imply that it's about weaker orders. But I still think we don't need a tag about "relaxed" atomics. It only has 27 questions, 1 rust the rest c++. It can mean
std::memory_order_relaxed
(no ordering wrt. operations on other objects) or depending on who's using it, can mean any order weaker thanseq_cst
.memory-model - For questions on memory ordering models at the programming language level (above the ISA or machine language level). This is about what the rules are for memory ordering, atomicity, thread-safety, and so on. Fairly related, but perhaps worthy of being a separate tag. language-lawyer questions about memory-ordering rules often use this tag, but there are plenty of questions without language-lawyer.
Several uses of this tag are about hardware / ISA memory models; perhaps we should relax the description to allow those usages, or tag such questions memory-order?
memory_order
parameters; and certainly not the [memory-ordering] tag which is another synonym. C and C++ (and I guess Rust) are the main languages I know of that have an actual language feature calledmemory_order
, but that doesn't mean we can't have questions about (reasoning about memory ordering in) other languages sharing the same tag.