shared_ptr在C ++ 11中的原子操作 [英] atomic operations for shared_ptr in C++11

查看:1032
本文介绍了shared_ptr在C ++ 11中的原子操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读c ++ 11草稿 n3242 ,第20.7.2.5节,看起来像我们在shared_ptr上有原子操作,这使得我们可以对复杂的结构进行无锁操作,而不用担心GC /内存泄漏。

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.

我不能在GCC-4.7.0中成功使用它。我只是测试下面的程序

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);
}

并且有编译器错误:

c.cpp:13:33: error: no matching function for call to ‘atomic_load(std::shared_ptr<X>*)’

有没有人知道我在这里错过了什么?或者是简单的gcc没有实现呢?

Does anyone know what I missed here? Or is it simply gcc hasn't implemented that yet?

推荐答案

有一个建议废除这些atomic_store / atomic_load方法shared_ptr赞成新的atomic_shared_ptr类: http:/ /www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4162.pdf

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

所以,当你得到一个libstc ++版本与atomic_load它可能很好地包含新的原子指针已经,这是更好的几个方面。

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已经实现了这些方法一段时间,libc ++声明完整的C ++ 11/14合规性,因此它们必须在其最新版本中可用。

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.

这篇关于shared_ptr在C ++ 11中的原子操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆