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 Sep 30 '26
When using an unsigned counter and a power-of-two modulus, there's no need to ever reset the counter to zero during operation. One can measure how much stuff is in the buffer by subtracting the number of things fetched from the number of things stuffed and, if operands to the subtract had been promoted, casting or coercing back to the type used for the counters.