Search code examples
c++11shared-ptratomic

atomic_store of shared_ptr of interface


I am trying to set the value of a shared_ptr in an atomic way:

shared_ptr<Base> a = std::make_shared<Derived>();
....
shared_ptr<Base> b;
std::atomic_store(&b,a); // Error here

I got the error message "'std::shared_ptr< Base >' is not derived from 'volatile std::atomic<_ITp>'"

How to fix this? Thanks.


Solution

  • There is a specialization of atomic_store for std::shared_ptr, see

    http://en.cppreference.com/w/cpp/memory/shared_ptr/atomic

    And by modern compilers (say, GCC 5) the code you provide is compiled just fine. So I suppose you compiler is not fully support C++11 (just like GCC 4.x, that lacks the specialization).