Daily bit(e) of C++ | std::atomic<std::shared_ptr>, std::atomic<std::weak_ptr>
Daily bit(e) of C++ #467, The C++20 thread safe version of std::shared_ptr and std::weak_ptr: std::atomic<std::shared_ptr>, std::atomic<std::weak_ptr>.
While the std::shared_ptr has limited use in single-threaded code, in multi-threaded code, the notion of shared ownership (even if temporary) is fairly common.
However, while multiple instances of std::shared_ptr that point to the same memory can be managed safely in multi-threaded code, operations on a single instance of a std::shared_ptr are not thread-safe.
This is why C++20 introduced std::atomic<std::shared_ptr<T>> and std::atomic<std::weak_ptr<T>>, which can be used to implement thread-safe shared data structures.
Unfortunately, the GCC libstdc++ implementation is not lock-free (Clang libc++ doesn't implement it), so the resulting performance can be disappointing.