Difference between revisions of "cpp/memory/shared ptr/atomic"
m (apply P2869R3 (C++26): Remove Deprecated shared_ptr Atomic Access APIs) |
(Added LWG issue #2172 DR.) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 79: | Line 79: | ||
@4@ Equivalent to {{c|atomic_store_explicit(p, r, std::memory_order_seq_cst)}}. | @4@ Equivalent to {{c|atomic_store_explicit(p, r, std::memory_order_seq_cst)}}. | ||
− | @5@ Stores the shared pointer {{c|r}} in the shared pointer pointed-to by {{c|p}} atomically, | + | @5@ Stores the shared pointer {{c|r}} in the shared pointer pointed-to by {{c|p}} atomically, {{lt|cpp/language/as if}} by {{c|p->swap(r)}}. |
@@ As with the non-specialized {{lc|std::atomic_store_explicit}}, if {{c|mo}} is {{c|std::memory_order_release}} or {{c|std::memory_order_acq_rel}}, the behavior is undefined. | @@ As with the non-specialized {{lc|std::atomic_store_explicit}}, if {{c|mo}} is {{c|std::memory_order_release}} or {{c|std::memory_order_acq_rel}}, the behavior is undefined. | ||
@6@ Equivalent to {{c|atomic_exchange_explicit(p, r, std::memory_order_seq_cst)}}. | @6@ Equivalent to {{c|atomic_exchange_explicit(p, r, std::memory_order_seq_cst)}}. | ||
− | @7@ Stores the shared pointer {{c|r}} in the shared pointer pointed to by {{c|p}} and returns the value formerly pointed-to by {{c|p}}, atomically | + | @7@ Stores the shared pointer {{c|r}} in the shared pointer pointed to by {{c|p}} and returns the value formerly pointed-to by {{c|p}}, atomically, {{lt|cpp/language/as if}} by {{c|p->swap(r)}} and returns a copy of {{c|r}} after the swap. |
@8@ Equivalent to | @8@ Equivalent to | ||
Line 128: | Line 128: | ||
The [[cpp/experimental/concurrency|Concurrency TS]] offers atomic smart pointer classes {{tt|atomic_shared_ptr}} and {{tt|atomic_weak_ptr}} as a replacement for the use of these functions. | The [[cpp/experimental/concurrency|Concurrency TS]] offers atomic smart pointer classes {{tt|atomic_shared_ptr}} and {{tt|atomic_weak_ptr}} as a replacement for the use of these functions. | ||
− | {{rrev| | + | {{rrev multi|since1=c++20|until1=c++26|rev1= |
These functions were deprecated in favor of the specializations of the {{lc|std::atomic}} template: {{c/core|std::atomic<std::shared_ptr>}} and {{c/core|std::atomic<std::weak_ptr>}}. | These functions were deprecated in favor of the specializations of the {{lc|std::atomic}} template: {{c/core|std::atomic<std::shared_ptr>}} and {{c/core|std::atomic<std::weak_ptr>}}. | ||
+ | |rev2= | ||
+ | These functions were removed in favor of the specializations of the {{lc|std::atomic}} template: {{c/core|std::atomic<std::shared_ptr>}} and {{c/core|std::atomic<std::weak_ptr>}}. | ||
}} | }} | ||
Line 137: | Line 139: | ||
===Defect reports=== | ===Defect reports=== | ||
{{dr list begin}} | {{dr list begin}} | ||
− | {{dr list item|wg=lwg|dr=2980|std=C++11|before=empty {{tt|shared_ptr}}s | + | {{dr list item|wg=lwg|dr=2172|std=C++11|before={{c|expected}} could be a null pointer|after=the behavior is undefined in this case}} |
+ | {{dr list item|wg=lwg|dr=2980|std=C++11|before=empty {{tt|shared_ptr}}s were never equivalent|after=equivalent if they store the same pointer value}} | ||
{{dr list end}} | {{dr list end}} | ||
Latest revision as of 23:04, 20 October 2024
Defined in header <memory>
|
||
template< class T > bool atomic_is_lock_free( const std::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( const std::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 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, expected, desired, std::memory_order_seq_cst,
std::memory_order_seq_cst).
(p, expected, desired, std::memory_order_seq_cst,
std::memory_order_seq_cst).
- If they are equivalent (store the same pointer value, and either share ownership of the same object or are both empty), assigns desired into *p using the memory ordering constraints specified by success and returns true.
- If they are not equivalent, assigns *p into *expected using the memory ordering constraints specified by failure and returns false.
atomic_compare_exchange_weak_explicit
may fail spuriously.If p is a null pointer, the behaviors of these functions are all undefined.
Contents |
[edit] 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 |
[edit] Exceptions
These functions do not throw exceptions.
[edit] Return value
[edit] 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 std::atomic template: std::atomic<std::shared_ptr> and std::atomic<std::weak_ptr>. |
(since C++20) (until C++26) |
These functions were removed in favor of the specializations of the std::atomic template: std::atomic<std::shared_ptr> and std::atomic<std::weak_ptr>. |
(since C++26) |
[edit] Example
This section is incomplete Reason: no example |
[edit] 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 2172 | C++11 | expected could be a null pointer | the behavior is undefined in this case |
LWG 2980 | C++11 | empty shared_ptr s were never equivalent
|
equivalent if they store the same pointer value |
[edit] 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) |
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) |