Skip to content

Built to Disappear

Edit source

An atomic shared_ptr that becomes a one-line alias the moment the standard library grows its own.

Part 24 of 38ofFamiliars
  1. 1Session Zero
  2. 2Rolling for a Name
  3. 3The Haunted Handler
  4. 4The Sparse Wheel
  5. 5The Fork in the Path
  6. 6The Planet as a Prefix
  7. 7The Long Division
  8. 8The Line You Usually Cancel
  9. 9Sixteen Bytes Too Far
  10. 10Packing a UUID
  11. 11Bytes You Can Type
  12. 12The Shunting Yard
  13. 13Interchangeable Parts
  14. 14One Loop Per Core
  15. 15The Seam
  16. 16Append Only
  17. 17Who Owns the Tree
  18. 18Down the Flume
  19. 19The Syscalls That Lie
  20. 20Asking the Machine
  21. 21The Earth Is Not a Ball
  22. 22Smith and Schmidt
  23. 23Many Ways to Be Similar
  24. 24Built to Disappear
  25. 25Sometimes a Mutex Is Right
  26. 26Work, Not Threads
  27. 27Before the Program Runs
  28. 28No Collisions, Ever
  29. 29The Names Enums Throw Away
  30. 30The Bag of Holding
  31. 31A Map of Blocks
  32. 32Certainly Absent
  33. 33A Tree Made of Types
  34. 34Everything but the Views
  35. 35Who Owns the Wheel
  36. 36A Guest on the Loop
  37. 37Acknowledged Means Durable
  38. 38Sorted as Bytes

Part of Familiars: the small, sharp libraries I carved out of Xapiand. This one is designed to become unnecessary: atomic-shared-ptr.

A shared_ptr is thread-safe in exactly one respect: the reference count. Two threads can copy and destroy their own copies of one shared_ptr concurrently and the count stays correct. What is not safe is two threads racing on the pointer itself, one reading it while another reassigns it, which is exactly what you need to do to publish a new version of some shared state without a lock of your own: build the new thing, then atomically swap the pointer so readers either see the whole old value or the whole new one, never a torn half. That atomic swap is the primitive under a lot of lock-free design, the read-copy-update pattern, a config you hot-reload, the head of a lock-free list, and a plain shared_ptr cannot do it.

atomic-shared-ptr provides it: atomic_shared_ptr<T>, with the same member API as std::atomic, load, store, exchange, compare_exchange_weak and _strong, is_lock_free, so your code reads and CAS-swaps a shared pointer the same way it would an integer. The usual retry loop that swaps in a new head looks like this:

atomic_shared_ptr<Node> head{std::make_shared<Node>()};
auto cur = head.load();
auto next = std::make_shared<Node>();
while (!head.compare_exchange_weak(cur, next)) { /* rebuild from cur, retry */ }

The genuinely nice thing about this one is not what it does, it is how it plans to stop existing. C++20 (proposal P0718) added a standard std::atomic<std::shared_ptr<T>> specialization that does precisely this. Not every standard library shipped it, though; libc++, the one under Apple clang, still lacks the specialization (Apple clang 17 included), even as it keeps the older pre-C++20 free-function atomic_* overloads on shared_ptr. So there is a gap, some compilers have the clean standard thing, some do not, and code that wants to be portable today has to bridge it.

atomic-shared-ptr bridges it by retiring itself gracefully. It checks the feature-test macro __cpp_lib_atomic_shared_ptr, and:

  • where the standard library implements the specialization, atomic_shared_ptr<T> is a plain alias for std::atomic<std::shared_ptr<T>>, and the header adds nothing at all;
  • where it is missing, the header provides the implementation itself, built on the older free-function overloads.

So the library is a polyfill with a built-in exit. On a modern toolchain it compiles down to the standard type and evaporates; on an older one it quietly does the work; and your code says atomic_shared_ptr<T> either way and never has to know which branch it got. That is the right way to write a shim for a feature the standard is in the middle of absorbing: do not entrench yourself, make yourself deletable. The day the last compiler you care about grows the specialization, this dependency becomes a one-line using and then nothing, and no call site has to change.

Reach for it when you need atomic operations on a shared_ptr, publishing snapshots of shared state such as a hot-swapped configuration, on a codebase that must build across compilers not all of which have the C++20 specialization yet. Skip it if you target only toolchains that ship std::atomic<std::shared_ptr<T>> (then just use that), if a mutex around your pointer is simple enough and not on a hot path, or if the path must be truly lock-free: on libc++ and libstdc++ neither branch is, and is_lock_free() returns false.

The next familiar is the honest counterweight to this whole lock-free chapter: a queue that does not try to be clever, pays a mutex without apology, and spends its cleverness instead on the two things lock-free queues make hardest, timeouts and a clean shutdown.