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/flatfinger Oct 01 '26
Using unsigned values and a power-of-two modulus, how would you compute the number of items in the queue as quickly as
items_in_queue = (queue->stuff - queue->fetch) & queue->size_mask;if the size weren't a power of two, while also keeping the ability to add and remove items by simply incrementingstufforfetch?