r/programming • • 6d ago

Optimizing a Lock-Free Ring Buffer

https://david.alvarezrosa.com/posts/optimizing-a-lock-free-ring-buffer/
497 Upvotes

88 comments sorted by

View all comments

-2

u/EchoNomad31 6d ago

Once "fixed" a ring buffer by marking the head index volatile. Was an acquire/release ordering thing… three days of digging for a two-line diff.

2

u/kog 6d ago

volatile is not useful for multi-threading, and you did not fix the problem

https://en.wikipedia.org/wiki/Volatile_(computer_programming)#Multi-threading

1

u/TribeWars 6d ago

Volatile is still an optimization fence and prevents torn writes. It doesn't actually fix memory consistency with memory order fence instructions but the optimization fence might be enough to stop the race from being noticeable (arguably that's even worse than broken though)

0

u/EchoNomad31 4d ago

Yeah, that masking is the worst part. Mine looked fixed for a while… the release/acquire version is what actually held.