|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Modifiers | ||||
| Observers | ||||
(C++17) | ||||
(until C++20*) | ||||
(C++26) | ||||
(C++26) | ||||
| Non-member functions | ||||
(until C++20)(until C++20)(until C++20)(until C++20)(until C++20)(C++20) | ||||
atomic_xxx functions(until C++26*) | ||||
| Helper classes | ||||
(C++20) | ||||
| Deduction guides(C++17) |
Defined in header <memory> | ||
template<class T> bool atomic_is_lock_free(conststd::shared_ptr<T>* p); | (1) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> std::shared_ptr<T> atomic_load(conststd::shared_ptr<T>* p); | (2) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> std::shared_ptr<T> atomic_load_explicit | (3) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> void atomic_store(std::shared_ptr<T>* p,std::shared_ptr<T> r); | (4) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> void atomic_store_explicit | (5) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> std::shared_ptr<T> atomic_exchange | (6) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> std::shared_ptr<T> atomic_exchange_explicit | (7) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> bool atomic_compare_exchange_weak | (8) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> bool atomic_compare_exchange_strong | (9) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> bool atomic_compare_exchange_strong_explicit | (10) | (since C++11) (deprecated in C++20) (removed in C++26) |
template<class T> bool atomic_compare_exchange_weak_explicit | (11) | (since C++11) (deprecated in C++20) (removed in C++26) |
If multiple threads of execution access the samestd::shared_ptr object without synchronization and any of those accesses uses a non-const member function ofshared_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 ashared_ptr is thread-safe: differentstd::shared_ptr objects can be accessed using mutable operations, such asoperator= orreset, simultaneously by multiple threads, even when these instances are copies, and share the same control block internally.
atomic_compare_exchange_weak_explicit may fail spuriously.Ifp is a null pointer, the behaviors of these functions are all undefined.
Contents |
| p, expected | - | a pointer to astd::shared_ptr |
| r, desired | - | astd::shared_ptr |
| mo, success, failure | - | memory ordering selectors of typestd::memory_order |
These functions do not throw exceptions.
These functions are typically implemented using mutexes, stored in a global hash table where the pointer value is used as the key.
TheConcurrency TS offers atomic smart pointer classesatomic_shared_ptr andatomic_weak_ptr as a replacement for the use of these functions.
These functions were deprecated in favor of the specializations of thestd::atomic template:std::atomic<std::shared_ptr> andstd::atomic<std::weak_ptr>. | (since C++20) (until C++26) |
These functions were removed in favor of the specializations of thestd::atomic template:std::atomic<std::shared_ptr> andstd::atomic<std::weak_ptr>. | (since C++26) |
| This section is incomplete Reason: no example |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2172 | C++11 | expected could be a null pointer | the behavior is undefined in this case |
| LWG 2980 | C++11 | emptyshared_ptrs were never equivalent | equivalent if they store the same pointer value |
(C++11) | checks if the atomic type's operations are lock-free (function template)[edit] |
(C++11)(C++11) | atomically replaces the value of the atomic object with a non-atomic argument (function template)[edit] |
(C++11)(C++11) | atomically obtains the value stored in an atomic object (function template)[edit] |
(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)[edit] |
| 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)[edit] |