Re:I've been writing ring buffers wrong all these years
by mark-r on Wednesday December 14, 2016 @01:40PM
(#13178473)
One of the benefits of the original algorithm is the independence of the read and write indexes, they can be updated from different threads (or different processors!) without any atomic operations beyond writing or reading a value. Subtracting from both pointers requires an additional atomic read/modify/write operation.
[ Parent
| on HN ]
Re:I've been writing ring buffers wrong all these years
by Guvante on Wednesday December 14, 2016 @03:00PM
(#13179153)
You could also just restrict the pointers in the normal way but to two times the size of the buffer. So instead of wrapping at N you wrap at 2*N.
You are only encoding 1 bit of data (first or second) so adding more data than that by allowing unsigned integer overflow is just an optimization, not fundamentally necessary.