CKTIS / C++
Atomic load of `std::shared_ptr` with explicit memory order
In C++23, this deprecated overload atomically copies a `std::shared_ptr` and lets you choose the memory order of the load.
Version: ISO C++23; implementation support may vary
Syntax
// ISO C++23 — declared in <memory>
namespace std {
template<class T>
shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order order);
}
// For std::shared_ptr<T> slot:
auto snapshot = std::atomic_load_explicit(&slot, std::memory_order_acquire);std::atomic_load_explicit(shared_ptr) denotes the free-function overload declared in <memory>. Pass &slot, the address of a live std::shared_ptr<T> object, not the address of its pointee or of a std::atomic<T>. It returns a std::shared_ptr<T> copy by value without throwing. The copy retains shared ownership if the loaded value had it. Passing a null address for slot is undefined behavior; the pointer stored in slot may nevertheless be null, so check the result before dereferencing if that is possible.
In C++23, the valid load orders are std::memory_order_relaxed, std::memory_order_consume, std::memory_order_acquire, and std::memory_order_seq_cst. Using std::memory_order_release or std::memory_order_acq_rel is undefined behavior. The shorthand std::atomic_load(&slot) uses std::memory_order_seq_cst.
If an acquire load reads the value written to the same slot by std::atomic_store_explicit with std::memory_order_release, the store synchronizes with the load. Initialization done before the store is then visible to code after the load. A separate std::atomic_thread_fence(std::memory_order_release) before that store is redundant for this publication. A std::memory_order_relaxed load is still atomic, but does not provide this synchronization on its own.
If multiple threads access the same slot and any of them may modify it, do not mix this API with ordinary copies, assignments, or calls to reset() on that slot unless the accesses are separately synchronized. For example, an ordinary assignment or call to reset() in one thread can race with an atomic load in another. Atomic loading does not protect later changes to the pointee and is not guaranteed to be lock-free. This overload was introduced in C++11, deprecated in C++20, retained in C++23, and removed from the C++26 standard. For new code, use std::atomic<std::shared_ptr<T>> with load(order) and store(value, order).
Examples
#include <atomic>
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<const int> slot;
std::atomic_store_explicit(&slot, std::make_shared<const int>(42),
std::memory_order_release);
const auto snapshot = std::atomic_load_explicit(&slot, std::memory_order_acquire);
std::atomic_store_explicit(&slot, std::shared_ptr<const int>{},
std::memory_order_relaxed);
std::cout << *snapshot << '\n';
}Result
42
The single-threaded program prints `42` because `snapshot` retains shared ownership after `slot` is cleared. It demonstrates syntax and lifetime, not synchronization between threads.
Related lessons
- C++ · Lesson 37 · Advanced · Version: 3
Sources
- C++23 final working draft (N4950) — shared_ptr atomic access [depr.util.smartptr.shared.atomic]
- WG21 P2869R0 — Remove Deprecated shared_ptr Atomic Access APIs From C++26
- C++ working draft — Annex D compatibility changes from ISO C++ 2023 [diff.cpp23.depr]
- C++ working draft — Order and consistency [atomics.order]
- C++ working draft — Fences [atomics.fences]
- C++ working draft — Partial specialization for shared_ptr [util.smartptr.atomic.shared]