News for hackers. Stuff that matters.
Re:Optimizing a Lock-Free Ring Buffer by jitl on Thursday March 26, 2026 @09:58AM (#47530537)
Really? Pretty much all atomics i’ve used have load, store of various integer sizes. I wrote a ring buffer in Go that’s very similar to the final design here using similar atomics.

https://pkg.go.dev/sync/atomic#Int64

Re:Optimizing a Lock-Free Ring Buffer by dalvrosa on Thursday March 26, 2026 @10:19AM (#47530805)
Nice one, thanks for sharing. Do you wanna share the ring buffer code itself?
Re:Optimizing a Lock-Free Ring Buffer by wat10000 on Thursday March 26, 2026 @10:23AM (#47530846)
They generally map directly to concepts in the CPU architecture. On many architectures, load/store instructions are already guaranteed to be atomic as long as the address is properly aligned, so atomic load/store is just a load/store. Non-relaxed ordering may emit a variant load/store instruction or a separate barrier instruction. Compare-exchange will usually emit a compare and swap, or load-linked/store-conditional sequence. Things like atomic add/subtract often map to single instructions, or might be implemented as a compare-exchange in a loop.

The exact syntax and naming will of course differ, but any language that exposes low-level atomics at all is going to provide a pretty similar set of operations.

Re:Optimizing a Lock-Free Ring Buffer by jitl on Thursday March 26, 2026 @11:50AM (#47531988)
yeah that’s why i was surprised by grandparent saying the atomics were c++ specific
Re:Optimizing a Lock-Free Ring Buffer by dalvrosa on Thursday March 26, 2026 @10:56AM (#47531273)
100% agree +1
Re:Optimizing a Lock-Free Ring Buffer by pjdesno on Friday March 27, 2026 @12:17PM (#47544690)
What's not guaranteed for "normal" loads and stores on many architectures is the order in which writes become visible to other CPU cores.
Re:Optimizing a Lock-Free Ring Buffer by wat10000 on Friday March 27, 2026 @01:51PM (#47545980)
Right, that's what the various memory ordering constants are for in C++ atomics, and other languages will likely have an equivalent. On such architectures, those will emit special instructions or barriers.