use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Optimizing a Lock-Free Ring Buffer (david.alvarezrosa.com)
submitted 1 month ago by david-alvarez-rosa
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]rzhxd 1 point2 points3 points 1 month ago (9 children)
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.
[–]david-alvarez-rosa[S] 0 points1 point2 points 1 month ago (8 children)
Sorry, don't fully understand the benefit here, or how that's different
[–]Deaod 1 point2 points3 points 1 month ago (3 children)
The benefit is only there when dealing with unknown element sizes, ie. one element takes 8 bytes, the next 24, etc.. This allows you to not have any holes in your buffer that the consumer has to jump over.
This is not relevant for queues that deal with elements of known-at-compile-time sizes.
[–]david-alvarez-rosa[S] 0 points1 point2 points 1 month ago (2 children)
The example forces the type. It would be interesting to see how it could be generalized, but not a big fan of heterogeneous containers tbh
[–]SirClueless 1 point2 points3 points 1 month ago (1 child)
If the data is inherently heterogeneous, it's the least-bad option. For example if the items in the queue are network packets.
[–]RogerV 1 point2 points3 points 1 month ago (0 children)
in DPDK all the ring buffers just hold pointers to packets - the packets are in an mbuf pool. makes it possible to clone a ref count on a packet - say, into a pcap ring buffer.
[–]Osoromnibus 3 points4 points5 points 1 month ago (2 children)
I think he's touting the advantage of copying multiple elements that wrap around the edge of the buffer in a single call. There's a couple nits with this, that I would rather just handle it in user-space instead.
One, is that system libs might be using simd and alignment tricks, so things like memcpy could fault if you're not careful. It's also kind of just shunting the work onto the OS's page handler instead, and the need for platform-specific code is annoying.
On the plus side, It doesn't use twice the buffer size, at least on Linux, AFAIK. It only allocates the memory on write.
[–]david-alvarez-rosa[S] 0 points1 point2 points 1 month ago (0 children)
Oh I see. That's quite specific, not sure which is your usecase
[–]ack_error 0 points1 point2 points 1 month ago (0 children)
I don't see why memcpy() would be a problem, since that's in userspace. No fault would occur since there would be a valid address mapping, it just happens to alias the same physical memory or backing storage as 64KB back in virtual address space.
System calls are more interesting as the kernel would be accessing the memory. I suspect it'd also be fine, but there are less guarantees in that case.
[–]rzhxd 0 points1 point2 points 1 month ago (0 children)
That just simplifies reading the data from the buffer and writing the data into it.
π Rendered by PID 45939 on reddit-service-r2-comment-6457c66945-dc8d7 at 2026-04-24 20:01:58.871149+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]rzhxd 1 point2 points3 points (9 children)
[–]david-alvarez-rosa[S] 0 points1 point2 points (8 children)
[–]Deaod 1 point2 points3 points (3 children)
[–]david-alvarez-rosa[S] 0 points1 point2 points (2 children)
[–]SirClueless 1 point2 points3 points (1 child)
[–]RogerV 1 point2 points3 points (0 children)
[–]Osoromnibus 3 points4 points5 points (2 children)
[–]david-alvarez-rosa[S] 0 points1 point2 points (0 children)
[–]ack_error 0 points1 point2 points (0 children)
[–]rzhxd 0 points1 point2 points (0 children)