3

I have seen that std::atomic<shared_ptr<T>> and std::atomic<weak_ptr<T>> will be applied to C++20. What are these and how can i use them?

Mert Köklü
  • 2,183
  • 2
  • 16
  • 20
  • I thought `shared_ptr` and `weak_ptr` were already atomic in nature with regards to reference counting the underlying object and access across multiple threads. Or does this support for atomic simply make the pointer swaps themselves atomic across assignment changes? – selbie Nov 25 '19 at 02:53
  • @selbie The reference count of `shared_ptr` is atomic, but the `shared_ptr` itself isn't without this. #Maceline: selbie's comment would probably make for a better question. Assuming one knows what `std::atomic` and `std::shared_ptr` do, there should be no problem understanding `std::atomic>`, so this question seems too broad to me. A concrete question regarding a misunderstanding such as selbie's would make for a better question imho. – walnut Nov 25 '19 at 02:57
  • `std::experimental::atomic_shared_ptr` was merged into C++20 as `std::atomic>`. – walnut Nov 25 '19 at 03:03
  • @selbie Btw. I didn't mean to say that you have a misunderstanding. You are completely correct that this is meant to make the modification of the pointer held by `std::shared_ptr` atomic. What I meant is that it may be a common misunderstanding whether atomicity of `shared_ptr` only applies to the reference count, not the `shared_ptr` object itself. – walnut Nov 25 '19 at 03:06

0 Answers0