Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atomic operations for shared_ptr in C++11

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?

like image 629
Kan Li Avatar asked Jun 09 '12 00:06

Kan Li


2 Answers

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.

like image 159
Max Galkin Avatar answered Oct 19 '22 08:10

Max Galkin


Looks like it's just not in libstdc++ yet.

It's also not in libc++

VS11 beta has it. I think this is the first thing I've found in VS11's library that wasn't in libc++.

like image 43
bames53 Avatar answered Oct 19 '22 06:10

bames53