std::atomic...hange_strong(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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 the atomic_ref object πŸ‘‘.
  • 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 be std::memory_order_release or std::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