std::experi...r::exchange(3)

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

πŸ’– NAME πŸ’–

okay so bestie, this page is from the C++ Standard Library and it’s filed under std::experi...r::exchange(3) β€” the actual star of the show is std::experimental::atomic_shared_ptr::exchange. And yes, the NAME section is literally std::experimental::atomic_shared_ptr::exchange β€” std::experimental::atomic_shared_ptr::exchange. iconic, no notes πŸ’…

✨ Synopsis ✨

here’s the signature, bestie:

shared_ptr<T> exchange( shared_ptr<T> desired, std::memory_order order = std::memory_order_seq_cst ) noexcept;

It atomically replaces the underlying shared_ptr with desired. This is a read-modify-write operation, and memory is affected according to the value of order. 🫢

πŸŽ€ Parameters πŸŽ€

  • desired β€” value to assign. literally the new shared_ptr going in.
  • order β€” memory order constraints to enforce. the signature has it defaulting to std::memory_order_seq_cst.

πŸ’… Return value πŸ’…

The value of the atomic variable before the call. that’s the return value, periodt πŸ’–

🧚 Remarks 🧚

  • All changes to the atomic_shared_ptr object itself, and all associated use_count increments, are guaranteed to be performed atomically.
  • Associated use_count decrements take place after the atomic operation, but are not required to be part of it.
  • Any associated destruction or deallocation operations take place after the atomic operation and are not part of it.
ngl this is the important caveat: the atomicity covers changes to the atomic_shared_ptr object itself and all associated use_count increments. The decrements happen after the atomic operation but aren’t required to be part of it, and any associated destruction or deallocation happens after the atomic operation too β€” not part of it. πŸ’€

🌈 See also 🌈

the page note says specializes atomic operations for std::shared_ptr (function template), and here’s the whole squad:

  • std::atomic_is_lock_free(std::shared_ptr)
  • std::atomic_load(std::shared_ptr)
  • std::atomic_load_explicit(std::shared_ptr)
  • std::atomic_store(std::shared_ptr)
  • std::atomic_store_explicit(std::shared_ptr)
  • std::atomic_exchange(std::shared_ptr)
  • std::atomic_exchange_explicit(std::shared_ptr)
  • std::atomic_compare_exchange_weak(std::shared_ptr)
  • std::atomic_compare_exchange_strong(std::shared_ptr)
  • std::atomic_compare_exchange_weak_explicit(std::shared_ptr)
  • std::atomic_compare_exchange_strong_explicit(std::shared_ptr)

also on the page: (deprecated in C++20) and (removed in C++26). so yeah, keep that in mind, bestie 😭


okay bye! source/version: http://cppreference.com, date: 2024.06.10, page: std::experi...r::exchange(3). slay πŸ’…βœ¨

πŸ“¦ where this page comes from