Search code examples
c++gccshared-ptratomic

alternative to std::atomic_store(shared_ptr) for GCC <= 4.9?


I tried something similar what's described in this question:

// create copy
auto new_data(std::make_shared<some_class>(*this->data));

... // modify *new_data

// apply new data
std::atomic_store(&this->data, new_data);

.. and later I realized that on some other machine with gcc 4.8 installed it would not work (like described in the same question).

Is there a workaround - like (temporarily) deriving from std::shared_ptr providing the needed interface or just using another atomic operation?

Or do I have to provide an external mutex to protect the reading access when I store a new object in the global pointer?


Solution

  • One option is of course to just use boost::shared_ptr and boost::atomic_store instead of STL if Boost is an option. It has the same interface and therefore it's easy to replace.