An introduction to lockless algorithms
An introduction to lockless algorithms
Posted Feb 20, 2021 6:15 UTC (Sat) by itsmycpu (guest, #139639)In reply to: An introduction to lockless algorithms by itsmycpu
Parent article: An introduction to lockless algorithms
>
> As long as the value read in step3 is the value written in step2 (or a value written after that). Because then you know that step3 > step2, and because of that, step4 > step1.
To clarify the not-so-obvious part:
From the point of view of the first thread, step2 > step1 is always true, but without using acquire/release, this doesn't transfer to the second thread. Without that, the second thread may not see step4 > step1.
Why not? For example, the compiler is allowed to produce code for step2 that affects memory before step1, as long as that doesn't change the outcome of the first thread's local computations. The compiler does not have to consider the possible side effect on the second thread, which therefore might see the memory affected by step2 before it is affected by step1.
Only the use of acquire/release requires the compiler to produce code where the update of step1 is written to memory, and therefore visible to other threads, before step2. (Memory or shared cache). So without that, the second thread cannot assume step4 > step1, even if it thinks it can assume step3 > step2.
I think this is the same thing as the article says, just highlighting the practical aspects.
