|
|
|
|
|
by falcolas
3477 days ago
|
|
Usually when I'm writing a ring buffer, it's for tasks where the loss of an item is acceptable (even desirable - a destructive ring buffer for debugging messages is a fantastic tool). As such, I simply push the read indicator when I get to the r=1, w=1 case. Using the mask method is slick (I'd cache that mask with the array to reduce runtime calculations), but it's definitely going to add cognitive overhead and get messy if you want to make it lockless with CAS semantics. |
|
So, store size-1 instead of size, and add one when asked for the size? I can see that, though I'm not confident it's worth the conceptual overhead.
If you mean storing it in addition to the size, I think that's a bad trade - cache is far more precious than many decrements.
Of course, if the size is fixed at compile time, the mask will probably be stored baked into the instructions (andl <const>, ...).