atomic operations for shared_ptr in C++11

c++, c++11, shared-ptr

Solution

There is a proposal to deprecate these atomic_store/atomic_load methods for shared_ptr in favor of the new atomic_shared_ptr class: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4162.pdf

So by the time you get a libstc++ version with atomic_load it may very well contain the new atomic pointers already, which are better in several aspects.

MSVC STL has implemented these methods a while ago, and libc++ also claims full C++11/14 compliance, so they must be available in its latest versions.

Problem

By reading the c++11 draft n3242, section 20.7.2.5, looks like we have atomic operations on shared_ptr, which enables us do lock-free on complicated structure without worrying about GC/memory leak. However, I couldn't use it successfully in GCC-4.7.0. I simply tested the following program ``` #include <atomic> #include <memory> #include <string> struct X { int x; double y; std::string s; }; int main() { std::shared_ptr<X> x(new X); auto p = std::atomic_load(&x); } ``` and it has compiler error: ``` c.cpp:13:33: error: no matching function for call to ‘atomic_load(std::shared_ptr<X>*)’ ``` Does anyone know what I missed here? Or is it simply gcc hasn't implemented that yet?

Original source