C++ std::atomic 与 Boost atomic

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9551750/
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-08-27 12:58:17  来源:igfitidea点击:

C++ std::atomic vs. Boost atomic

c++multithreadingatomic

提问by 2607

In my application, I have an int and a bool variable, which are accessed (multiple write/read) by multiple threads. Currently, I am using two mutexes, one for int and one for bool to protect those variables.

在我的应用程序中,我有一个 int 和一个 bool 变量,它们由多个线程访问(多次写入/读取)。目前,我使用了两个互斥锁,一个用于 int,一个用于 bool 来保护这些变量。

I heard about using atomic variables and operators to write lock-free multi-thread program. My questions are

我听说过使用原子变量和运算符编写无锁多线程程序。我的问题是

  1. What's the definition of atomic variables and operators?
  2. What's the main difference between std::atomicand boost/atomic.hpp? Which one is more standard or popular?
  3. Are these libraries platform-dependent? I am using gnu gcc 4.6 on Linux at the moment, but ideally it shall be cross-platform. I heard that the definition of "atomic" actually depends on the hardware as well. Can anyone explain that as well?
  4. What's the best way to share a bool variable among multiple threads? I would prefer not to use the "volatile" keyword.
  1. 原子变量和运算符的定义是什么?
  2. std::atomicboost/atomic.hpp之间的主要区别是什么?哪个更标准或更受欢迎?
  3. 这些库是否依赖于平台?我目前在 Linux 上使用 gnu gcc 4.6,但理想情况下它应该是跨平台的。我听说“原子”的定义实际上也取决于硬件。谁能解释一下?
  4. 在多个线程之间共享 bool 变量的最佳方法是什么?我不想使用“ volatile”关键字。

Are these code thread-safe?

这些代码是线程安全的吗?

double double_m; // double_m is only accessed by current thread.
std::atomic<bool> atomic_bool_x;
atomic_bool_x = true && (double_m > 12.5);

int int_n; // int_n is only accessed by current thread.
std::atomic<int> atomic_int_x;
std::atomic<int> atomic_int_y;
atomic_int_y = atomic_int_x * int_n;

采纳答案by Pubby

I'm not an expert or anything, but here's what I know:

我不是专家或任何东西,但这是我所知道的:

  1. std::atomicsimply says that calling loadand store(and a few other operations) concurrently is well-defined. An atomic operation is indivisible - nothing can happen 'in-between'.
  2. I assume std::atomicis based off of boost::atomic. If you can, use std, otherwise use boost.
  3. They are both portable, with the stdbeing completely so, however your compiler will need to support C++11
  4. Likely std::atomic_bool. You should not need to use volatile.
  1. std::atomic简单地说,同时调用loadstore(以及一些其他操作)是明确定义的。原子操作是不可分割的——“中间”不会发生任何事情。
  2. 我假设std::atomic是基于boost::atomic. 如果可以,请使用std,否则使用boost
  3. 它们都是可移植的,std完全如此,但是您的编译器需要支持 C++11
  4. 可能std::atomic_bool。您不应该需要使用 volatile。

Also, I believe load/storediffers from operator=/operator Tonly load/storeare atomic.

另外,我相信load/store不同于operator=/ operator Tonly load/ storeare atomic

Nevermind. I checked the standard and it appears that the operators are defined in terms of load/store/etc, however they may return different things.

没关系。我检查了标准,似乎运算符是根据load/ store/etc定义的,但是它们可能返回不同的东西。

Further reading:

进一步阅读:

回答by Andrew Khosravian

Volatile is orthogonal to what you use to implement atomics. In C++ it tells the compiler that certain it is not safe to perform optimizations with that variable. Herb Sutters lays it out:

Volatile 与你用来实现原子的东西是正交的。在 C++ 中,它告诉编译器使用该变量执行优化是不安全的。赫伯·萨特斯 (Herb Sutters) 阐述了这一点:

To safely write lock-free code that communicates between threads without using locks, prefer to use ordered atomic variables: Java/.NET volatile, C++0x atomic, and C-compatible atomic_T.

To safely communicate with special hardware or other memory that has unusual semantics, use unoptimizable variables: ISO C/C++ volatile. Remember that reads and writes of these variables are not necessarily atomic, however.

Finally, to express a variable that both has unusual semantics and has any or all of the atomicity and/or ordering guarantees needed for lock-free coding, only the ISO C++0x draft Standard provides a direct way to spell it: volatile atomic.

为了安全地编写在不使用锁的情况下在线程之间进行通信的无锁代码,最好使用有序的原子变量:Java/.NET volatile、C++0x atomic 和 C-compatible atomic_T。

要安全地与具有异常语义的特殊硬件或其他内存通信,请使用不可优化的变量:ISO C/C++ volatile。但是请记住,这些变量的读取和写入不一定是原子的。

最后,为了表达一个既具有异常语义又具有无锁编码所需的任何或所有原子性和/或排序保证的变量,只有 ISO C++0x 草案标准提供了一种直接的拼写方法: volatile atomic .

(from http://drdobbs.com/article/print?articleId=212701484&siteSectionName=parallel)

(来自http://drdobbs.com/article/print?articleId=212701484&siteSectionName=parallel

回答by cooky451

  1. See std::atomic class template
  2. std::atomicis standard since C++11, and the Boost stuff is older. But since it is standard now, I would prefer std::atomic.
  3. ?? You can use std::atomicwith each C++11 compiler on each platform you want.
  4. Without any further information...

    std::atomic;

  1. 参见std::atomic 类模板
  2. std::atomic自 C++11 以来是标准的,而 Boost 的东西更旧。但既然它现在是标准的,我更喜欢std::atomic.
  3. ?? 您可以std::atomic在您想要的每个平台上使用每个 C++11 编译器。
  4. 没有任何进一步的信息...

    标准::原子;

回答by 01100110

I believe std::atomic(C++11) and boost.atomicare equivalent. If std::atomicis not supported by your compiler yet, use boost::atomic.

我相信std::atomic(C++11) 并且boost.atomic是等价的。如果std::atomic您的编译器尚不支持,请使用boost::atomic.