xcode 带有自定义类的 std::atomic (C++ 11)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15886308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 03:10:01  来源:igfitidea点击:

std::atomic with custom class (C++ 11)

c++xcodec++11atomic

提问by yvan vander sanden

I am using std::atomic with a custom class in my library. All works fine with MSVC, but now that i'm trying to get it to run on macOS, i get a linker error:

我在库中使用带有自定义类的 std::atomic。MSVC 一切正常,但现在我试图让它在 macOS 上运行,我收到一个链接器错误:

undefined symbols for architecture x86_64: "__atomic_store", referenced from: _main in main.o

架构 x86_64 的未定义符号:“__atomic_store”,引用自:main.o 中的 _main

I've created some test code to replicate this

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

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

Of course this example doesn't make much sense, but it's the shortest I could come up with. It does run in VS2012, but not in xcode (giving me the linker error shown above).

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

So what's going on? Am I abusing std::atomic here? The library I'm working on is heavily multithreaded and used for audio processing. So if I'm not using std::atomic in the correct way, it is not really showing, because performance is very good and I don't have any threading problems with it. Or is xcode perhaps lacking something?

发生什么了?我在这里滥用 std::atomic 吗?我正在开发的库是多线程的,用于音频处理。因此,如果我没有以正确的方式使用 std::atomic,它就不会真正显示出来,因为性能非常好,而且我没有任何线程问题。或者 xcode 可能缺少什么?

Update:

更新:

I've checked andrey's answer because it has the most information, although all 3 answers are useful. I'm no expert in this (obviously) but it seems that VS2012 goes beyond what should be implemented in C++11.

我已经检查了 andrey 的答案,因为它包含最多的信息,尽管所有 3 个答案都很有用。我不是这方面的专家(显然),但似乎 VS2012 超出了应该在 C++11 中实现的内容。

So how to go from here? I see a few options.

那么从这里怎么走呢?我看到了几个选项。

  1. Don't use atomic for this class. In my particular case this would be very hard because my vector class is used all over the code. Locking and unlocking mutexes would probably slow things down a lot.
  2. Implement the functions myself for atomic. That looks very complicated to me. I'll save it as the last option.
  3. See if something can be done with boost::atomic. This seems to work at first glance. I have to do more tests on it though.
  1. 不要在这个类中使用 atomic 。在我的特殊情况下,这将非常困难,因为我的向量类在整个代码中使用。锁定和解锁互斥体可能会大大减慢速度。
  2. 自己为原子实现函数。这对我来说看起来很复杂。我将其保存为最后一个选项。
  3. 看看是否可以用 boost::atomic 做些什么。乍一看,这似乎有效。不过,我必须对其进行更多测试。

采纳答案by Andrey Volk

As described in http://en.cppreference.com/w/cpp/atomic/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

标准库为以下类型提供了 std::atomic 模板的完整特化:

1) bool 类型及其 typedef 的一种特化
2) 整型的特化和 typedef
3) 所有指针类型的 std::atomic

What about the Boost.Atomic. As described in Boost.Atomic limitations:

Boost.Atomic怎么样。如Boost.Atomic 限制中所述

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

使用非 POD 类作为原子的模板参数会导致未定义的行为。

回答by inkooboo

Check which standard library you are using in project pagein clang compiler settings. It should be GNU's libstdc++ with c++11 support or libc++.

检查您在clang 编译器设置中的项目页面中使用的标准库。它应该是支持 c++11 的 GNU 的 libstdc++ 或 libc++。

Hope it'll help.

希望它会有所帮助。