mercredi 21 juin 2023

C Different Memory Order meaning in simple terms with example

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.

Aucun commentaire:

Enregistrer un commentaire