Inside STL: Waiting for a std::atomic to change, part 1 -- Raymond Chen
When using std::atomic<std::shared_ptr<T>>, the C++ standard defines a "change" as a modification to either the stored pointer or the control block pointer. However, since atomic wait mechanisms typically track only a single memory address, the Microsoft implementation handles this limitation by using a timeout-based polling strategy to detect changes in the control block.
Inside STL: Waiting for a std::atomic<std::shared_ptr<T>> to change, part 1
by Raymond Chen
From the article:
Like other
std::atomicspecializations,std::atomic<supports thestd::shared_ptr<T>> waitandnotify_*methods for waiting for the value to change and reporting that the value has changed. The definition of “changed” in the C++ language specification is that the value has changed if either the stored pointer or the control block pointer has changed. A shared pointer is implemented as a pair of pointers, butWaitOnAddresscan wait on at most 8 bytes, and unix futexes can wait on only four bytes, so how does this work?¹The Microsoft implementation waits for the stored pointer to change, and the
notify_*methods signal the stored pointer. But wait, this fails to detect the case where the stored pointer stays the same and only the control block changes.std::atomic<std::shared_ptr<int>> p = std::make_shared<int>(42); void change_control_block() { auto old = p.load(); auto empty = std::shared_ptr<int>(); // Replace with an indulgent shared pointer // with the same stored pointer. p.store({ empty, old.get() }); p.notify_all(); } void wait_for_change() { auto old = p.load(); p.wait(old); }

Since its introduction, the
Sometimes, we all need a way to iterate over a container in the opposite direction. There are several ways to reverse-iterate a container, and in this article, we’ll explore them.
In this article, you’ll see eight larger examples that illustrate the changes in C++23.
Back in the day, being a witch was considered a grave crime. Today, we’re diving into one of C++’s lesser-known spells: ADL (Argument-Dependent Lookup).