0

At start a and b are 0

  • In thread 1,atomic write a=1 then atomic read b
  • In thread 2,atomic write b=1 then atomic read a

If I want to make sure thread 1 can read b=1 or thread 2 can read a=1,which memory order should I use,and why?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • To me it sounds like you don't need atomics at all, just use message passing instead of shares variables. – JHBonarius Oct 19 '21 at 08:40
  • 2
    You need `memory_order_seq_cst` (the default) to prevent the case where they both read 0. This is the classic StoreLoad reordering litmus test. https://preshing.com/20120515/memory-reordering-caught-in-the-act/. Looking for a duplicate... (Unless you mean that you want to also rule out them both loading 1? You can't rule that out with any memory ordering, since it's possible with a simple interleaving of source orders.) – Peter Cordes Oct 19 '21 at 08:47

0 Answers0