std::atomic...it_explicit(3)
π NAME π
Welcome to the std::atomic...it_explicit(3) page from the C++ Standard Libary manual (yes, that spelling is literally in the header, not my typo π). This whole vibe is about std::atomic_flag_wait and std::atomic_flag_wait_explicit, bestie.
β¨ Synopsis β¨
First, the header tea:
Defined in header <atomic>
Then the overloads, all since C++20:
- (1) (since C++20)
void atomic_flag_wait( const atomic_flag* object, bool old ) noexcept;
- (2) (since C++20)
void atomic_flag_wait( const volatile atomic_flag* object, bool old ) noexcept;
- (3) (since C++20)
void atomic_flag_wait_explicit( const atomic_flag* object, bool old, std::memory_order order ) noexcept;
- (4) (since C++20)
void atomic_flag_wait_explicit( const volatile atomic_flag* object, bool old, std::memory_order order ) noexcept;
These perform atomic waiting operations. No cap.
Here's the actual mechanic: it compares object->test(std::memory_order_seq_cst) or object->test(order) with old, and if they are equal then it blocks until *object is notified by std::atomic_flag::notify_one() or std::atomic_flag::notify_all() (or the thread is unblocked spuriously). This is repeated until the values compare unequal. The way that loop just keeps checking? Iconic.
- 1,2) the memory synchronization order is
std::memory_order_seq_cst. - 3,4) the memory synchronization order is
order.
Iforderis one ofstd::memory_order_releaseandstd::memory_order_acq_rel, the behavior is undefined. So do not do that, bestie. It's giving UB π.
These functions are guaranteed to return only if value has changed, even if underlying implementation unblocks spuriously. Slay for correctness β¨.
π Parameters π
objectβ pointer to the atomic flag to check and wait on.oldβ the value to check the atomic flag no longer contains.orderβ the memory synchronization ordering.
π«Ά Return value π«Ά
(none) β literally nothing comes back, bestie. It's a void function moment.
πΈ Notes πΈ
This form of change-detection is often more efficient than simple polling or pure spinlocks. Lowkey iconic.
ABA problem warning: due to the ABA problem, transient changes from old to another value and back to old might be missed, and not unblock. So yeah, it can get ghosted by a sneaky ABA cycle π.
π¦ Example π¦
This section is incomplete.
Reason: no example.
π See also π
notify_oneβ notifies at least one thread waiting on the atomic object (C++20) (public member function ofstd::atomic_flag).notify_allβ notifies all threads blocked waiting on the atomic object (C++20) (public member function ofstd::atomic_flag).atomic_flag_notify_oneβ notifies a thread blocked inatomic_flag_wait(C++20) (function).atomic_flag_notify_allβ notifies all threads blocked inatomic_flag_wait(C++20) (function).
π Category π
* Todo no example
Sign-off szn π: source/version http://cppreference.com, date 2024.06.10, page std::atomic...it_explicit(3). Love u, bye bestie π
π¦ 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