23

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 tag because is currently a synonym for .

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) and from + .

Or better, reverse things so 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 .

Using memory barriers/fences is one way to achieve ordering, so is the more general concept we're collecting together, currently under the tag name. I think the best end result would be to have all 4 of those tags still be synonyms, but of . (If that's easily possible.)


In case anyone's looking at similarly named tags:

  • is about a separate concept: a thread-synchronization building block where threads that call barrier(foo) sleep until all threads have called barrier(foo), then they all wake up. This is unrelated to memory ordering and fences.

  • 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 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 than seq_cst.

  • - 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. 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 ?

9
  • 3
    Never thought a synonym request on Meta would teach me a thing or two about memory order.
    – rene
    Commented Aug 19, 2022 at 5:20
  • 2
    @rene: Yeah, while I was writing this, I was thinking "this seems pretty technical for meta", but the reason for this proposed change is rooted in the technical terminology and how to think about the subject. I decided not to hold back since the title is there to warn people away if they don't know about these tags at all. But yeah, I figured presenting it in a way that could educate would be useful to get everyone on the same page, among other things :) Commented Aug 19, 2022 at 5:33
  • 1
    If removing the memory barrier tag from a post, just make sure that multiple users aren't fixing it at the same time :P
    – Lundin
    Commented Aug 19, 2022 at 10:06
  • On a serious note, I'm not sure if "memory order" is a common technical term outside C++? If not it should indeed be an exclusive tag not at all related to the memory-barrier tag.
    – Lundin
    Commented Aug 19, 2022 at 10:06
  • @Lundin: Hmm, since you mention existing posts, that reminds me: I guess this change wouldn't improve the tags on them until their next edit. Hopefully they'd still show up in searches on [memory-order] if we flip synonyms. Also, re: multiple users editing: ordering can't create atomicity, and atomicity doesn't require ordering wrt. operations on other objects, only a modification order for this atomic object :P But yeah there's a joke in there somewhere. Commented Aug 19, 2022 at 10:11
  • Either way I think tweaking these might require someone with domain knowledge to manually go through the 653 existing posts. Doesn't sound like a very sexy task, since it might be too technical to call on the usual burnination crew.
    – Lundin
    Commented Aug 19, 2022 at 10:15
  • @Lundin: I thought [memory-order] had relevance for other languages (other than C and C++). That's an interesting point to investigate. That proposal (to de-synonymize into memory-barriers+memory fences vs. memory-order+memory-ordering) should probably be an answer, so it can get voted on separately from my answer. Commented Aug 19, 2022 at 10:19
  • At least I associate "memory order" with the atomics update in C11/C++11, but then I'm biased (and normally not even using these features). Some languages like Rust appear to have inherited similar features from C++11 though.
    – Lundin
    Commented Aug 19, 2022 at 10:25
  • 1
    @Lundin: It can make sense to ask questions about ordering of memory operations in [java] or C#, even though only effectively seq_cst ordering is available. I don't think [memory-order] was supposed to only be about stdatomic 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 called memory_order, but that doesn't mean we can't have questions about (reasoning about memory ordering in) other languages sharing the same tag. Commented Aug 19, 2022 at 10:31

1 Answer 1

10

As proposed in the question, IMO the best way to sort out these tags is:

Posting this as an answer so it can get voted separately from the fact that there's a problem and something should change.

1
  • It seems users can't undo synonyms, only propose them. According to stackoverflow.com/tags/synonyms, @CodyGray created the synonym in the first place as a moderator, and he hangs around in CPU-architecture tags. Commented Aug 24, 2022 at 10:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .