std::atomic...ator&=,|=,^=(3)

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

πŸ’… std::atomic...ator&=,|=,^=(3) πŸ’…

okay so bestie, welcome to the std::atomic...ator&=,|=,^=(3) page from the C++ Standard Libary manual. yes, that spelling is exactly what's on the page β€” I don't make the rules πŸ’€. this one is all about std::atomic_ref's compound assignment operators &=, |=, and ^=, and it's giving atomic bitwise main character energy. let's get into it 🫢✨


🌸 NAME 🌸

std::atomic_ref::operator&=,|=,^= β€” std::atomic_ref::operator&=,|=,^=

yes bestie, the name section is literally the same thing twice. iconic, honestly.

✨ Synopsis ✨

member only of atomic_ref<Integral> specializations
T operator&=( T arg ) const noexcept;               (1) (since C++20)
T operator|=( T arg ) const noexcept;               (2) (since C++20)
T operator^=( T arg ) const noexcept;               (3) (since C++20)

so what's the vibe? it atomically replaces the current value of the referenced object with the result of a computation involving the previous value and arg. these operations are read-modify-write operations, periodt.

here's the breakdown:

  • operator&= β€” performs atomic bitwise AND. Equivalent to return fetch_and(arg) & arg;.
  • operator!= β€” performs atomic bitwise OR. Equivalent to return fetch_or(arg) | arg;. (yes, the page says operator!=, not me 😭 I'm just reporting what's on the sheet.)
  • operator^= β€” performs atomic bitwise XOR. Equivalent to return fetch_xor(arg) ^ arg;.

πŸ“ Parameters πŸ“

  • arg β€” the argument for the arithmetic operation.

πŸŽ€ Return value πŸŽ€

the resulting value β€” that is, the result of applying the corresponding binary operator to the value immediately preceding the effects of the corresponding member function. yes, all of that is the return value, bestie πŸ’…


πŸ’œ Notes πŸ’œ

unlike most compound assignment operators, the compound assignment operators for atomic_ref do not return a reference to their left-hand arguments. they return a copy of the stored value instead. so if you were expecting a reference, surprise 🫢


πŸ“ Example πŸ“

This section is incomplete

Reason: no example

not the "no example" era 😭 it's fine, we're still slaying.

πŸ‘‘ See also πŸ‘‘

  • fetch_and β€” atomically performs bitwise AND between the argument and the value of the referenced object and obtains the value held previously (public member function)
  • fetch_or β€” atomically performs bitwise OR between the argument and the value of the referenced object and obtains the value held previously (public member function)
  • fetch_xor β€” atomically performs bitwise XOR between the argument and the value of the referenced object and obtains the value held previously (public member function)
  • operator++, operator++(int), operator--, operator--(int) β€” atomically increments or decrements the referenced object by one (public member function)
  • operator+=, operator-= β€” atomically adds to or subtracts from the referenced value (public member function)

🏷️ Category 🏷️

  • Todo no example

source/version & date check, bestie: shoutout to http://cppreference.com, dated 2024.06.10, page std::atomic...ator&=,|=,^=(3). go atomically slay today πŸ’–βœ¨πŸ¦‹

πŸ“¦ where this page comes from