· 01:48
The article discusses the introduction of std::atomic<shared_ptr<T>>
in C++20, which adds atomicity to standard shared pointers. A normal shared_ptr
uses two pointers: one to the actual object and another to a control block containing reference counts and other metadata. The atomic version modifies the control block pointer to utilize the bottom two bits for flags, specifically for a lock mechanism. The implementations from glibc++ and MSVC differ in how they handle locking and waiting for the lock to clear, with one using spinlocks and the other threading notifications. Overall, atomic shared pointers enable safe concurrent access but with some performance trade-offs, especially in mostly-read scenarios.
Key Points:
std::atomic<shared_ptr<T>>
for atomic operations on shared pointers.shared_mutex
may offer better performance compared to atomic shared pointers.std::atomic<shared_ptr<T>>
. Listen to jawbreaker.io using one of many popular podcasting apps or directories.