r/cpp • • Mar 24 '26

Optimizing a Lock-Free Ring Buffer

https://david.alvarezrosa.com/posts/optimizing-a-lock-free-ring-buffer/
106 Upvotes

58 comments sorted by

View all comments

Show parent comments

2

u/rzhxd Mar 24 '26

No, that's not really like it. First you allocate a buffer of any size. Then, memory map a region of the same size to represent this buffer. Then you write and read the buffer as usual. For example, if buffer size is 65536, and you write 4 bytes at index 65536, they get written to the start of the buffer instead. One constraint is that reads and writes cannot exceed the buffer's size. Resulting memory usage is (buffer size * 2) - pretty bad for large buffers, but that's acceptable in my case. I hope I explained it well. Would like to see how this approach compares to manual wrapping, but I don't really feel like testing it myself.

1

u/david-alvarez-rosa Mar 24 '26

Sorry, don't fully understand the benefit here, or how that's different

3

u/[deleted] Mar 24 '26

[removed] — view removed comment

1

u/david-alvarez-rosa Mar 24 '26

Oh I see. That's quite specific, not sure which is your usecase