0

I was trying to grasp different memory_order meanings in a simple term and understand it in details. I have read through https://en.cppreference.com/w/cpp/atomic/memory_order and to some extent understand the usage of some of these for synchronization. I do understand that memory_order_seq_cst do guarantee strictness around load and store, which would mean no surprises but at an overhead and there can be cases when even mutex would be faster than these depending on scenario. I wanted to understand the meaning of each of these memory_order and their significance in simple terms memory_order_relaxed, memory_order_consume, memory_order_acquire, memory_order_release, memory_order_acq_rel, memory_order_seq_cst. E.g in case of memory_order_relaxed compiler can move statements below and above that atomic variable with memory_order_relaxed. So below could result into an output of 37 0

 Global
 atomic<int> x, y;

 Thread 1                            Thread 2
 x.store(17,memory_order_relaxed);   cout << y.load(memory_order_relaxed) << " ";
 y.store(37,memory_order_relaxed);   cout << x.load(memory_order_relaxed) << endl;`

But the same example with memory order modified to memory_order_release and memory_order_acquire makes sure that output cannot be 37 0 ever.

 Global
 atomic<int> x, y;

 Thread 1                            Thread 2
 x.store(17,memory_order_release);   cout << y.load(memory_order_acquire) << " ";
 y.store(37,memory_order_release);   cout << x.load(memory_order_acquire) << endl;

I want to understand these memory_order variables in a better way so that I am more comfortable using them instead of using sequence consistency most of the time which would degrade the performance.

Note that i did looked at C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming? SO answer.

Invictus
  • 4,028
  • 10
  • 50
  • 80
  • 1
    `cout<<` is probably going to take and release a lock, so there are very few if any real systems where LoadLoad reordering would be possible in practice even with `relaxed` loads. Load into `int tmpx, tmpy` locals and then output those, then (on non-x86 CPUs), it's very possible that you'd get LoadLoad reordering. (https://preshing.com/20120710/memory-barriers-are-like-source-control-operations/) – Peter Cordes Jun 22 '23 at 02:19
  • 1
    Re: acquire and release, see https://preshing.com/20120913/acquire-and-release-semantics/ – Peter Cordes Jun 22 '23 at 02:20
  • I don't think you actually stated a question. What are you asking here? Are you just asking for confirmation of whether your two examples behave as you said (the answer is yes they do), or something else? – Nate Eldredge Jun 22 '23 at 23:39

0 Answers0