Atomics in C++26?
published at 04.12.2025 18:05 by Jens Weller
Save to Instapaper Pocket
Did you know there is an C++26 addition to <atomic>?
During this year I've got a bit more interested in atomics. It likely started with reading C++ Memory Management and interviewing the author Patrice Roy. Also watching the recording of Designing an SPSC Queue before Meeting C++ 2025, catching a part of "The two memory models" during the event and a related talk from CppCon after the conference made me more curious about the topic.
But than there is also always something what kept nagging at me, who uses these more advanced atomic features? So I did a survey on LinkedIn:

Creating this survey made me look into the cppreference page for atomic types. And mostly there seems to be the easy to use interface useful for counters and sharing variables atomically, and the more advanced features with not much in between. But looking closer, there is a new functionality for the advanced users coming with C++26: fetch_max and fetch_min for integral and pointer types:
atomically performs std::max/min between the argument and the value of the atomic object and obtains the value held previously
The interface is the type plus a memory_order, with the memory_order defaulting to std::memory_order_seq_cst: fetch_max(T val, memory_order = memory_..._cst). This seems not to be implemented in any compiler yet, though the integral/pointer types proposals mentions a clang based implementation. Though the compiler support page and cppreference in general seem to be frozen in the last months. Looking at cppstat.org reveals that C++26 seems to also support these for floating point types. Also atomic min/max is not a new idea, its just something that didn't make the initial cut I guess. And then finding the time and being able to get the needed support to add these to the C++ standard can be difficult. So its great to see that this effort is a success, the initial proposal is from 2016, and being picked up again in 2020.
Reading the proposals for the integral types and the floating point types is quite interesting. The pointer based versions of min and max do have to assume that you operate on the same array/object, e.g. that what you compare makes sense. This means, that unlike other atomic operations, this allows for undefined behavior (point 10). Both proposals provide numbers on performance.
The integral types proposal shows an atomic queue using max for setting the new back value after adding/enqueing a value to the queue, which also shows that these exist as free functions in the standard, not only as members of an atomic type:
std::atomic_fetch_max(&queue.back,i)
The floating point proposal also shows a graph on performance, "using a synthetic micro-benchmark that measures throughput in Giga Operations per Second (y-axis; logarithmic) as a function of the number of threads (x-axis; logarithmic) from 32 to 130'000 hardware threads on an NVIDIA GPU system.".
So this new feature will allow you to check if a value is bigger/smaller then the one held in an atomic variable, and in this case have it assigned to this atomic instance. Which is nice. Supported are integral, pointer and floating point types.
Join the Meeting C++ patreon community!
This and other posts on Meeting C++ are enabled by my supporters on patreon!