加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

std ::原子与自定义类(C 11)

发布时间:2020-12-15 01:38:38 所属栏目:百科 来源:网络整理
导读:我在我的库中使用std :: atomic与自定义类.一切都适用于MSVC,但是现在我试图让它运行在macOS上,我得到一个链接器错误: 架构的未定义符号x86_64: “__atomic_store”,引自: _主要在main.o 我创建了一些测试代码来复制这个 #include iostream#include atomi
我在我的库中使用std :: atomic与自定义类.一切都适用于MSVC,但是现在我试图让它运行在macOS上,我得到一个链接器错误:

架构的未定义符号x86_64:
“__atomic_store”,引自:
_主要在main.o

我创建了一些测试代码来复制这个

#include <iostream>
#include <atomic>

using namespace std;

class Vec {
    public:
    int x,y,z;
    Vec() { x = y = z = 0; }
};

std::atomic<Vec> x;


int main()
{
  Vec a;
  x = a;
  cin.get();
    return 0;
}

当然这个例子没有什么意义,但是我能想出的最短.它运行在VS2012,但不是在xcode(给我上面显示的链接器错误).

发生什么了?我在这里滥用std :: atomic吗?我正在研究的图书馆是多线程的,用于音频处理.所以如果我没有以正确的方式使用std :: atomic,它并不是真的显示,因为性能非常好,我没有任何线程问题.还是xcode可能缺少什么?

更新:

我已经检查了andrey的答案,因为它有最多的信息,虽然所有3个答案是有用的.我不是这个专家(显然),但似乎VS2012超出了C11中应该实现的内容.

那么怎么从这里走?我看到几个选项.

>不要为这个类使用atomic.在我特定的情况下,这将非常困难,因为我的向量类遍及代码.锁定和解锁互斥可能会减慢很多事情.
>自己实现原子的功能.对我来说看起来很复杂我会把它保存为最后一个选项.
>看看是否可以用boost :: atomic完成某些事情.这似乎乍一看.我必须做更多的测试.

解决方法

如 http://en.cppreference.com/w/cpp/atomic/atomic所述:

The standard library provides full specializations of the std::atomic
template for the following types:

1) One specialization for the type bool and its typedef
2)
Specializations and typedefs for integral types
3) std::atomic
for all pointer types

Boost.Atomic怎么样如Boost.Atomic limitations所述:

Using non-POD-classes as template parameter to atomic results in undefined behavior.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读