Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Atomically storing a std::shared_ptr with an explicit memory order

The legacy overload publishes a std::shared_ptr with a release store and pairs with an acquire load. It is still in C++23; prefer std::atomic<std::shared_ptr<T>> in new code.

Version: ISO C++23; implementation support may vary

Syntax

#include <atomic>
#include <memory>

namespace std {
    template<class T>
    void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> desired,
                               memory_order order);
}

std::atomic_store_explicit(shared_ptr) denotes the free-function overload in <memory> that takes the address of an ordinary std::shared_ptr<T>, not the address of a std::atomic<T>. It atomically replaces *p with the by-value desired, as if by p->swap(desired), and returns void. p must point to a live object, but the shared_ptr being stored may be empty. Do not assume the operation is lock-free.

During concurrent use, do not mix these free functions with direct reads, assignment, or reset() on the same shared instance. std::atomic_load_explicit returns a separate shared_ptr by value. Copies can share a control block and keep the managed object alive, but they do not prevent data races on mutable contents.

When a std::memory_order_acquire load reads the value written by a std::memory_order_release store, the store synchronizes with the load. Initialization before the store is then visible to code after the load. A std::atomic_thread_fence(std::memory_order_release) immediately before that store is redundant in this pattern. std::memory_order_relaxed preserves atomicity but does not by itself provide this publication guarantee. In C++23 the legacy store explicitly forbids acquire and acq_rel, and the legacy load forbids release and acq_rel; violating these preconditions is undefined behavior.

These free overloads were introduced in C++11, deprecated in C++20, and removed in C++26. They are still part of C++23; implementation support may vary. For new code, use std::atomic<std::shared_ptr<T>> and its store and load members.

Examples

#include <atomic>
#include <iostream>
#include <memory>
#include <thread>

struct Config {
    int limit;
};

int main() {
    std::shared_ptr<const Config> published;
    std::thread publisher([&] {
        auto next = std::make_shared<const Config>(Config{42});
        std::atomic_store_explicit(&published, next, std::memory_order_release);
    });

    std::shared_ptr<const Config> snapshot;
    while (!(snapshot = std::atomic_load_explicit(
                 &published, std::memory_order_acquire))) {
        std::this_thread::yield();
    }

    std::cout << snapshot->limit << '\n';
    publisher.join();
}

Result

42

The main thread repeats the acquire load until it gets a nonempty copy. It prints `42` before `join()`; the release/acquire pair, not `join()`, is what publishes the configuration.

Related lessons

  • C++ · Lesson 37 · Advanced · Version: 3

Sources