std::atomic...est_and_set(3)
π 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 whichorderyou're passing. the defaultstd::memory_order_seq_cstis 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
- 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