std::atomic...hange_strong(3)
π NAME π
okay bestie, welcome to the std::atomic...hange_strong(3) page from the C++ Standard Libary β¨ β yes, that manual name is literally spelled that way, no cap. This page is all about std::atomic_ref::compare_exchange_weak,std::atomic_ref::compare_exchange_strong π«Ά, and it's giving atomic compare-and-swap realness.
β¨ Synopsis β¨
Four overloads, all const and all noexcept β periodt π
. Every one of them is (since C++20).
bool compare_exchange_weak( T& expected, T desired,
std::memory_order success,
std::memory_order failure ) const
noexcept;
(1) (since C++20)
bool compare_exchange_weak( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst ) const
noexcept;
(2) (since C++20)
bool compare_exchange_strong( T& expected, T desired,
std::memory_order success,
std::memory_order failure ) const
noexcept;
(3) (since C++20)
bool compare_exchange_strong( T& expected, T desired,
std::memory_order order =
std::memory_order_seq_cst ) const
noexcept;
(4) (since C++20)
So here's the vibe π¦: it atomically compares the value representation of the referenced object with that of expected, and if those are bitwise-equal, it replaces the former with desired β that's a read-modify-write operation, bestie π₯. Otherwise, it loads the actual value stored in the referenced object into expected β that's a load operation, slay.
The memory models for the read-modify-write and load operations are success and failure respectively. In the (2) and (4) versions, order is used for both read-modify-write and load operations, except that std::memory_order_acquire and std::memory_order_relaxed are used for the load operation if order == std::memory_order_acq_rel, or order == std::memory_order_release respectively. literally the details matter, iykyk π.
πΈ Parameters πΈ
expectedβ reference to the value expected to be found in the object referenced by theatomic_refobject π.desiredβ the value to store in the referenced object if it is as expected π.successβ the memory synchronization ordering for the read-modify-write operation if the comparison succeeds. All values are permitted β¨.failureβ the memory synchronization ordering for the load operation if the comparison fails. Cannot bestd::memory_order_releaseorstd::memory_order_acq_relβ noted, bestie π«Ά.orderβ the memory synchronization ordering for both operations π .
π Return value π
true if the referenced object was successfully changed, false otherwise. simple as that, no cap π.
π¦ Notes π¦
The comparison and copying are bitwise β similar to std::memcmp and std::memcpy β and no constructor, assignment operator, or comparison operator are used. it's giving low-level, and we respect it π.
The weak forms (1,2) of the functions are allowed to fail spuriously, that is, act as if *this != expected even if they are equal. When a compare-and-exchange is in a loop, the weak version will yield better performance on some platforms β highkey useful info, bestie β.
When a weak compare-and-exchange would require a loop and a strong one would not, the strong one is preferable unless the object representation of T may include trap bits, or offers multiple object representations for the same value (e.g. floating-point NaN). In those cases, weak compare-and-exchange typically works because it quickly converges on some stable object representation. obsessed with a function that knows how to converge πβ¨.
For a union with bits that participate in the value representations of some members but not the others, compare-and-exchange might always fail because such padding bits have indeterminate values when they do not participate in the value representation of the active member. padding bit drama, honestly π.
Padding bits that never participate in an object's value representation are ignored. iconic behavior, honestly π.
π Example π
This section is incomplete Reason: no example
not me noticing there's literally no example π β the manual page said it, not me, bestie.
π§ Category π§
- Todo no example
okay that's the whole page, bestie β mwah π. source: http://cppreference.com, version/date: 2024.06.10, and the manual page is std::atomic...hange_strong(3) β periodt π
β¨.
π¦ 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