std::atomic...est_and_set(3)

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

πŸ’… std::atomic...est_and_set(3) β€” the C++ Standard Library girlies' guide

okay so bestie, welcome to the <atomic> corner of the standard library β€” this is std::atomic_flag::test_and_set, and honestly? she's iconic, she's tiny, she does exactly one thing and does it atomically ✨


🌸 NAME

std::atomic_flag::test_and_set β€” yes that's literally the whole name, and honestly it's giving "my function name is my whole personality" and I respect it πŸŽ€


✨ Synopsis

so here's the tea, this is all Defined in header <atomic> πŸ‘‘

Defined in header <atomic>
bool test_and_set( std::memory_order order =
                       std::memory_order_seq_cst ) volatile
(1) (since C++11)
noexcept;
bool test_and_set( std::memory_order order =
(2) (since C++11)
                       std::memory_order_seq_cst ) noexcept;

yep β€” two overloads, both (since C++11), both noexcept (she does NOT throw, periodt πŸ’€), and overload (1) is the volatile one while (2) is the non-volatile one. also notice the default arg is std::memory_order_seq_cst in both, so if you're lazy (me, always) you can just call her with no arguments at all 🫢


πŸ’– Description

so what does she do? she Atomically changes the state of a std::atomic_flag to set (true) and then returns the value it held before. that's it. that's the whole vibe. read-then-write, but like, atomically, so no other thread can sneak in between and ruin your whole life 😭πŸ”₯

literally the simplest compare-and-swap-adjacent queen in the whole header β€” no cap πŸ’…


πŸ¦‹ Parameters

  • order β€” the memory synchronization order. that's it, that's the whole parameter list, she travels light πŸŽ€

🌈 See also

  • clear β€” atomically sets flag to false (public member function)
  • atomic_flag_test_and_set, atomic_flag_test_and_set_explicit (C++11) β€” atomically sets the flag to true and returns its previous value (function)
  • memory_order (C++11) β€” defines memory ordering constraints for the given atomic operation (enum)

like ok bestie, clear and test_and_set are literally the duo of the century β€” one sets it true, one sets it false, and if you need the free-function version instead of the member, that's what atomic_flag_test_and_set and atomic_flag_test_and_set_explicit are for iykyk πŸ’œ and if you're confused about ordering at all, go read up on memory_order before you start vibing with seq_cst you don't understand πŸ™ˆ


fr fr though β€” this is atomic-land, so please actually think about which order you're passing. the default std::memory_order_seq_cst is the safest vibe check, but she's also not free. choose consciously, bestie πŸ’–

and that's the page!! sourced straight from http://cppreference.com, dated 2024.06.10, and this manual page is literally called std::atomic...est_and_set(3) πŸŽ€ slay, go forth and set some flags βœ¨πŸ¦‹πŸŒ·

πŸ“¦ where this page comes from