|
|
Subscribe / Log in / New account

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

Here's one way to think about it: A read barrier will order two reads with respect to each other, and a write barrier will order two writes. But not even the combination of a read barrier plus a write barrier will order a write with respect to a read. A full barrier, on the other hand, will.

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.


to post comments


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds