std::atomic_...(3)
π NAME π
std::atomic_... β std::atomic_... (yes bestie, the name is literally the name, and we're just gonna roll with it β¨).
Okay so this is std::atomic_...(3), from the C++ Standard Libary manual (typo included, no cap π), and we're gonna get you through every single overload. Grab a strawberry π and let's go.
π¦ Synopsis π¦
Defined in header <memory>.
All eleven overloads below are (since C++11), (deprecated in C++20), and (removed in C++26). Not the "deprecated and then removed" pipeline ππ
.
The overload lineup π
- Overload
(1)π
template< class T >
bool atomic_is_lock_free( const std::shared_ptr<T>* p );
- Overload
(2)π¦
template< class T >
std::shared_ptr<T> atomic_load( const std::shared_ptr<T>* p );
- Overload
(3)πΈ
template< class T >
std::shared_ptr<T> atomic_load_explicit
( const std::shared_ptr<T>* p, std::memory_order mo );
- Overload
(4)β¨
template< class T >
void atomic_store( std::shared_ptr<T>* p,
std::shared_ptr<T> r );
- Overload
(5)π
template< class T >
void atomic_store_explicit
( std::shared_ptr<T>* p, std::shared_ptr<T> r,
std::memory_order mo );
- Overload
(6)π
template< class T >
std::shared_ptr<T> atomic_exchange
( std::shared_ptr<T>* p, std::shared_ptr<T> r );
- Overload
(7)π¦
template< class T >
std::shared_ptr<T> atomic_exchange_explicit
( std::shared_ptr<T>* p, std::shared_ptr<T> r,
std::memory_order mo );
- Overload
(8)πΈ
template< class T >
bool atomic_compare_exchange_weak
( std::shared_ptr<T>* p, std::shared_ptr<T>* expected,
std::shared_ptr<T> desired );
- Overload
(9)β¨
template< class T >
bool atomic_compare_exchange_strong
( std::shared_ptr<T>* p, std::shared_ptr<T>* expected,
std::shared_ptr<T> desired );
- Overload
(10)π
template< class T >
bool atomic_compare_exchange_strong_explicit
( std::shared_ptr<T>* p, std::shared_ptr<T>* expected,
std::shared_ptr<T> desired,
std::memory_order success, std::memory_order failure
);
- Overload
(11)π
template< class T >
bool atomic_compare_exchange_weak_explicit
( std::shared_ptr<T>* p, std::shared_ptr<T>* expected,
std::shared_ptr<T> desired,
std::memory_order success, std::memory_order failure
);
The tea on each overload β
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.). That's the whole reason these little guys exist, bestie π«Ά.
Also 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. So the control block is lowkey doing its own thing and staying safe π
.
- Determines whether atomic access to the shared pointer pointed-to by
pis lock-free. - Equivalent to
atomic_load_explicit(p, std::memory_order_seq_cst). - Returns the shared pointer pointed-to by
p. As with the non-specializedstd::atomic_load_explicit, ifmoisstd::memory_order_releaseorstd::memory_order_acq_rel, the behavior is undefined. - Equivalent to
atomic_store_explicit(p, r, std::memory_order_seq_cst). - Stores the shared pointer
rin the shared pointer pointed-to bypatomically, equivalent top->swap(r). As with the non-specializedstd::atomic_store_explicit, ifmoisstd::memory_order_releaseorstd::memory_order_acq_rel, the behavior is undefined. - Equivalent to
atomic_exchange_explicit(p, r, std::memory_order_seq_cst). - Stores the shared pointer
rin the shared pointer pointed to bypand returns the value formerly pointed-to byp, atomically. Equivalent top->swap(r)and returns a copy ofrafter the swap. - Equivalent to
atomic_compare_exchange_weak_explicit(p, expected, desired, std::memory_order_seq_cst, std::memory_order_seq_cst). - Equivalent to
atomic_compare_exchange_strong_explicit(p, expected, desired, std::memory_order_seq_cst, std::memory_order_seq_cst).
10, 11. Compares the shared pointers pointed-to by 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.
Careful, bestie:atomic_compare_exchange_weak_explicitmay fail spuriously. Ifexpectedis a null pointer, orfailureisstd::memory_order_releaseorstd::memory_order_acq_rel, the behavior is undefined.
And one more big warning: If p is a null pointer, the behaviors of these functions are all undefined. Like, all of them. Do not pass null here, I am begging π.
πΈ Parameters πΈ
p,expectedβ a pointer to astd::shared_ptrr,desiredβ astd::shared_ptrmo,success,failureβ memory ordering selectors of typestd::memory_order
π Exceptions π
These functions do not throw exceptions. Periodt. π
π Return value π
- 1)
trueif atomic access is implemented using lock-free instructions. - 2,3) A copy of the pointed-to shared pointer.
- 4,5) (none)
- 6,7) A copy of the formerly pointed-to shared pointer.
- 8-11)
trueif the shared pointers were equivalent and the exchange was performed,falseotherwise.
π 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_ptrandatomic_weak_ptras a replacement for the use of these functions. - These functions were deprecated (since C++20) in favor of the specializations of the
std::atomictemplate:std::atomic<std::shared_ptr>and (until C++26)std::atomic<std::weak_ptr>. - These functions were removed in favor of the specializations of the
std::atomictemplate:std::atomic<std::shared_ptr>and (since C++26)std::atomic<std::weak_ptr>.
π Example π
This section is incomplete.
Reason: no example.
Category: * Todo no example
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
LWG 2980β applied to C++11. Behavior as published: emptyshared_ptrs are never equivalent if they store the same pointer value. Correct behavior: equivalent.
β See also β
atomic_is_lock_free(C++11) β checks if the atomic type's operations are lock-free (function template)atomic_store,atomic_store_explicit(C++11) β atomically replaces the value of the atomic object with a non-atomic argument (function template)atomic_load,atomic_load_explicit(C++11) β atomically obtains the value stored in an atomic object (function template)atomic_exchange,atomic_exchange_explicit(C++11) β atomically replaces the value of the atomic object with non-atomic argument and returns the old value of the atomic (function template)atomic_compare_exchange_weak,atomic_compare_exchange_weak_explicit,atomic_compare_exchange_strong,atomic_compare_exchange_strong_explicit(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)
Big kiss and a sparkle send-off: this page came from http://cppreference.com, version 2024.06.10, and it's the std::atomic_...(3) manual page. You ate that, bestie. πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms