On this page
std::atomic_...<std::shared_ptr>
Defined in header <memory> |
||
|---|---|---|
|
(1) | (since C++11) (deprecated in C++20) |
|
(2) | (since C++11) (deprecated in C++20) |
|
(3) | (since C++11) (deprecated in C++20) |
|
(4) | (since C++11) (deprecated in C++20) |
|
(5) | (since C++11) (deprecated in C++20) |
|
(6) | (since C++11) (deprecated in C++20) |
|
(7) | (since C++11) (deprecated in C++20) |
|
(8) | (since C++11) (deprecated in C++20) |
|
(9) | (since C++11) (deprecated in C++20) |
|
(10) | (since C++11) (deprecated in C++20) |
|
(11) | (since C++11) (deprecated in C++20) |
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 these functions, which are overloads of the corresponding atomic access functions (std::atomic_load, std::atomic_store, etc.).
Note that the control block of a shared_ptr is thread-safe: different 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.
p is lock-free.
atomic_load_explicit(p, std::memory_order_seq_cst).
p.
std::atomic_load_explicit, if mo is std::memory_order_release or std::memory_order_acq_rel, the behavior is undefined.
atomic_store_explicit(p, r, std::memory_order_seq_cst).
r in the shared pointer pointed-to by p atomically, equivalent to p->swap(r).
std::atomic_store_explicit, if mo is std::memory_order_release or std::memory_order_acq_rel, the behavior is undefined.
atomic_exchange_explicit(p, r, std::memory_order_seq_cst).
r in the shared pointer pointed to by p and returns the value formerly pointed-to by p, atomically. Equivalent to p->swap(r) and returns a copy of r after the swap.
atomic_compare_exchange_weak_explicit
(p, expected, desired, std::memory_order_seq_cst,
std::memory_order_seq_cst).
atomic_compare_exchange_strong_explicit
(p, expected, desired, std::memory_order_seq_cst,
std::memory_order_seq_cst).
p and expected.
- If they are equivalent (store the same pointer value, and either share ownership of the same object or are both empty), assigns
desiredinto*pusing the memory ordering constraints specified bysuccessand returnstrue. - If they are not equivalent, assigns
*pinto*expectedusing the memory ordering constraints specified byfailureand returnsfalse.
atomic_compare_exchange_weak_explicit may fail spuriously.
expected is a null pointer, or failure is std::memory_order_release or std::memory_order_acq_rel, the behavior is undefined.
If p is a null pointer, the behaviors of these functions are all undefined.
Parameters
| p, expected | - | a pointer to a std::shared_ptr |
| r, desired | - | a std::shared_ptr |
| mo, success, failure | - | memory ordering selectors of type std::memory_order |
Exceptions
These functions do not throw exceptions.
Return value
true if atomic access is implemented using lock-free instructions.
true if the shared pointers were equivalent and the exchange was performed, false otherwise.
Notes
These functions are typically implemented using mutexes, stored in a global hash table where the pointer value is used as the key.
The Concurrency TS offers atomic smart pointer classes atomic_shared_ptr and atomic_weak_ptr as a replacement for the use of these functions.
These functions were deprecated in favor of the specializations of the |
(since C++20) |
Example
Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2980 | C++11 | empty shared_ptrs are never equivalent |
equivalent if they store the same pointer value |
See also
|
(C++11)
|
checks if the atomic type's operations are lock-free (function template) |
|
(C++11)(C++11)
|
atomically replaces the value of the atomic object with a non-atomic argument (function template) |
|
(C++11)(C++11)
|
atomically obtains the value stored in an atomic object (function template) |
|
(C++11)(C++11)
|
atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template) |
|
(C++11)(C++11)(C++11)(C++11)
|
atomically compares the value of the atomic object with non-atomic argument and performs atomic exchange if equal or atomic load if not (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/memory/shared_ptr/atomic