vendredi 15 septembre 2023

Memory order around atomic operations

I want to build a good mental image about std::memory_order_seq_cst, std::memory_order_relaxed, memory_order_acq_rel.

Imagine a program as a sequence of bricks. Each brick is an operation on some variable. There are only two atomic variables x and y in the program. Atomic operations on x are colored red. Atomic operations on variable y are colored blue. The rest operations are uncolored --- in gray.

Sequential consistency std::memory_order_seq_cst is defined by the following constraints: (i) any blue or red brick will always stay at its original position, (ii) gray bricks between any two colored bricks might be reshuffled by the compiler within the said colored bricks.

The relaxed memory order std::memory_order_relaxed is almost the opposite of the sequential consistency: (i) the red (blue) bricks could be shifted in any manner as long as the order of the red (blue) sequence does not change.

Things get murkier to me about the acquire-release order std::memory_order_acq_rel. I tend to think they are both relaxations from sequential consistency: any colored brick can be shifted around, but it should never collide with another colored brick. In other words, if the code says atomic operation B happens between A and C, then the compiler should never shuffle the code in a way such that B would be executed before A or after C.

I made the following figures:

enter image description here

Figure 1 shows the order of operations in code. Figure 2 is one possibility under sequential consistency. Figure 3 is one possibility under relaxed memory order.

My questions are:

  1. Did I correctly understand sequential consistency and the relaxed memory order?

  2. Is my visualization accurate? Is there any better visualization?

  3. How to delineate acquire-release order with similar visualization?

I am currently not interested in std::memory_order_consume.

And yes, I have read https://gcc.gnu.org/wiki/Atomic/GCCMM/AtomicSync. But it's still hard for me to digest quickly. I am wondering if there is an even easier way to load this piece of knowledge in my brain after two years without touching the subject.

Thanks a lot!

Aucun commentaire:

Enregistrer un commentaire