Search⌘ K

Atomic Operations on std::shared_ptr

Explore how to perform thread-safe atomic operations on std::shared_ptr in modern C++. Understand the memory model implications, synchronization constraints, and future atomic smart pointers introduced in C++20. This lesson equips you with practical knowledge to safely manage shared pointers in concurrent programs.

We'll cover the following...

There are specializations for the atomic operations load, store, compare, and exchange for an std::shared_ptr. By using the explicit variant, you can even specify the memory model. Here are the free atomic operations for std::shared_ptr:

C++
std::atomic_is_lock_free(std::shared_ptr)
std::atomic_load(std::shared_ptr)
std::atomic_load_explicit(std::shared_ptr)
std::atomic_store(std::shared_ptr)
std::atomic_store_explicit(std::shared_ptr)
std::atomic_exchange(std::shared_ptr)
std::atomic_exchange_explicit(std::shared_ptr)
std::atomic_compare_exchange_weak(std::shared_ptr)
std::atomic_compare_exchange_strong(std::shared_ptr)
std::atomic_compare_exchange_weak_explicit(std::shared_ptr)
std::atomic_compare_exchange_strong_explicit(std::shared_ptr)

For the details, ...