原子存储抛出错误 [英] Atomic store throwing error

查看:145
本文介绍了原子存储抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近升级到一个C ++ 11兼容的编译器,我试图更新一些代码从boost到c ++ 11标准。我遇到了一个问题,当使用atomic_store转换一些代码。这里是一些简单的测试代码,似乎是为我抛出一个编译器错误。

I recently upgraded to a C++11 compatible compiler, and I was trying to update some code from boost to c++11 standards. I ran into a problem when converting some code using atomic_store over. Here is some simple test code that seems to be throwing a compiler error for me.

int main()
{
    std::shared_ptr<int> m = std::make_shared<int>();
    *m = 1;

    std::shared_ptr<int> a =  std::make_shared<int>();
    *a = 2;

    std::atomic_store(&m,std::move(a));

    std::cout << *m << std::endl;
}

std :: atomic_store(& m, std :: move(a)); 行正在为我抛出编译错误:

the std::atomic_store(&m,std::move(a)); line is throwing a compiler error for me:

'std::shared_ptr<int>' is not derived from 'volatile std::atomic<_ITp>'
     std::atomic_store(&m,std::move(a));
                                      ^

从boost升级到C ++ 11时,atomic_store有变化吗?

Has the way atomic_store changed when moving from boost to C++11? Do I now need to create an atomic object of a shared pointer?

推荐答案

下面的代码用Clang 3.5编译得很好:

The following code compiles fine with Clang 3.5:

#include <memory>
int main()
{
    std::shared_ptr<int> foo, bar;
    std::atomic_store(&foo, bar);
}

但是,它不能用GCC 4.9编译。上面的代码打印一个错误消息, atomic_store 不是 std 的成员。如果我还包括< atomic> ,编译器打印出错误消息,显示在问题中。

However, it doesn't compile with GCC 4.9. The above code prints an error message, that atomic_store is not a member of std. If I also include <atomic>, the compiler prints the error message, that is shown in the question.

显然,GCC 4.9不支持 std :: shared_ptr 的原子操作。另请参阅 libstdc ++ 的文档:

Apparently, GCC 4.9 does not support atomic operations for std::shared_ptr. See also the documentation of libstdc++:


20.7.2.5 | shared_ptr原子访问|部分

20.7.2.5 | shared_ptr atomic access | Partial

这篇关于原子存储抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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