An introduction to lockless algorithms
An introduction to lockless algorithms
Posted Feb 25, 2021 18:25 UTC (Thu) by Alan.Stern (subscriber, #12437)In reply to: An introduction to lockless algorithms by andresfreund
Parent article: An introduction to lockless algorithms
Example: Suppose the source code says:
write(x)
wmb()
rmb()
read(y)
Then the CPU has to execute the write before the wmb, and it has to execute the read after the rmb. But (depending on the architecture) nothing prevents it from reordering the operations to:
rmb()
read(y)
write(x)
wmb()
(In fact this actually happens on x86, where rmb and wmb are both no-ops and reads can be executed before earlier writes.)
However, with a full barrier:
write(x)
mb()
read(y)
no reordering is possible, and the write *must* be executed before the read.
There are other ways in which a full barrier is stronger than the combination of a read barrier plus a write barrier -- but at least this gets the point across.