atomic operations for shared_ptr in C++11

限于喜欢 提交于 2019-12-08 15:19:10

问题


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?


回答1:


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.




回答2:


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++.




回答3:


Use clang++ with -std=c++11 for compiling the code that should get it working.



来源:https://stackoverflow.com/questions/10957561/atomic-operations-for-shared-ptr-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!