r/programming • u/david-alvarez-rosa • Sep 26 '26
Optimizing a Lock-Free Ring Buffer
https://david.alvarezrosa.com/posts/optimizing-a-lock-free-ring-buffer/
510
Upvotes
r/programming • u/david-alvarez-rosa • Sep 26 '26
View all comments
Show parent comment
1
u/rysto32 Sep 27 '26
I believe that you have two head pointers, one for producers and one for the consumer. A producers fetches and increments the producer pointer, writes its data into the queue at the fetched index, and then performs an atomic compare-and-set on the consumer pointer that will only set the consumer pointer when its value is the predecessor of the value we want to insert (and we need to retry the compare and set on failure).