Namespaces
Variants
Actions

std::atomic<std::shared_ptr>

From cppreference.com
< cpp‎ | memory‎ | shared ptr
 
 
Memory management library
(exposition only*)
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
(until C++20*)
(until C++20*)

Garbage collector support (until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
(C++11)(until C++23)
 
 
Defined in header <memory>
template< class T >
struct std::atomic<std::shared_ptr<T>>;
(since C++20)

The partial template specialization of std::atomic for std::shared_ptr<T> allows users to manipulate shared_ptr objects atomically.

If multiple threads of execution access the same std::shared_ptr object without synchronization and any of those accesses uses a non-const member function of shared_ptr then a data race will occur unless all such access is performed through an instance of std::atomic<std::shared_ptr> (or, deprecated as of C++20, through the standalone functions for atomic access to std::shared_ptr).

Associated use_count increments are guaranteed to be part of the atomic operation. Associated use_count decrements are sequenced after the atomic operation, but are not required to be part of it, except for the use_count change when overriding expected in a failed CAS. Any associated deletion and deallocation are sequenced after the atomic update step and are not part of the atomic operation.

Note that the control block of a shared_ptr is thread-safe: different non-atomic std::shared_ptr objects can be accessed using mutable operations, such as operator= or reset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.

The type T may be an incomplete type.

Contents

[edit] Member types

Member type Definition
value_type std::shared_ptr<T>

[edit] Member functions

All non-specialized std::atomic functions are also provided by this specialization, and no additional member functions.

atomic<shared_ptr<T>>::atomic

constexpr atomic() noexcept = default;
(1)
constexpr atomic( std::nullptr_t ) noexcept : atomic() {}
(2)
atomic( std::shared_ptr<T> desired ) noexcept;
(3)
atomic( const atomic& ) = delete;
(4)
1,2) Initializes the underlying shared_ptr<T> to the null value.
3) Initializes the underlying shared_ptr<T> to a copy of desired. As with any std::atomic type, initialization is not an atomic operation.
4) Atomic types are not copy/move constructible.

atomic<shared_ptr<T>>::operator=

void operator=( const atomic& ) = delete;
(1)
void operator=( std::shared_ptr<T> desired ) noexcept;
(2)
void operator=( std::nullptr_t ) noexcept;
(3)
1) Atomic types are not copy/move assignable.
2) Value assignment, equivalent to store(desired).
3) Resets the atomic shared pointer to null pointer value. Equivalent to store(nullptr);.

atomic<shared_ptr<T>>::is_lock_free

bool is_lock_free() const noexcept;

Returns true if the atomic operations on all objects of this type are lock-free, false otherwise.

atomic<shared_ptr<T>>::store

void store( std::shared_ptr<T> desired,