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
C++ std::atomic vs. Boost atomic
提问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
我听说过使用原子变量和运算符编写无锁多线程程序。我的问题是
- What's the definition of atomic variables and operators?
- What's the main difference between std::atomicand boost/atomic.hpp? Which one is more standard or popular?
- 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?
- What's the best way to share a bool variable among multiple threads? I would prefer not to use the "volatile" keyword.
- 原子变量和运算符的定义是什么?
- std::atomic和 boost/atomic.hpp之间的主要区别是什么?哪个更标准或更受欢迎?
- 这些库是否依赖于平台?我目前在 Linux 上使用 gnu gcc 4.6,但理想情况下它应该是跨平台的。我听说“原子”的定义实际上也取决于硬件。谁能解释一下?
- 在多个线程之间共享 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:
我不是专家或任何东西,但这是我所知道的:
std::atomic
simply says that callingload
andstore
(and a few other operations) concurrently is well-defined. An atomic operation is indivisible - nothing can happen 'in-between'.- I assume
std::atomic
is based off ofboost::atomic
. If you can, usestd
, otherwise useboost
. - They are both portable, with the
std
being completely so, however your compiler will need to support C++11 - Likely
std::atomic_bool
. You should not need to use volatile.
std::atomic
简单地说,同时调用load
和store
(以及一些其他操作)是明确定义的。原子操作是不可分割的——“中间”不会发生任何事情。- 我假设
std::atomic
是基于boost::atomic
. 如果可以,请使用std
,否则使用boost
。 - 它们都是可移植的,
std
完全如此,但是您的编译器需要支持 C++11 - 可能
std::atomic_bool
。您不应该需要使用 volatile。
Also, I believe load
/store
differs from operator=
/operator T
only .load
/store
are atomic
另外,我相信load
/store
不同于operator=
/ operator T
only 。load
/ store
are 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:
进一步阅读:
- http://en.cppreference.com/w/cpp/atomic/atomic
- C++11 Standard
- C++ Concurrency in Action
- http://en.cppreference.com/w/cpp/atomic/atomic
- C++11 标准
- C++ 并发实战
回答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
- See std::atomic class template
std::atomic
is standard since C++11, and the Boost stuff is older. But since it is standard now, I would preferstd::atomic
.- ?? You can use
std::atomic
with each C++11 compiler on each platform you want. Without any further information...
std::atomic;
- 参见std::atomic 类模板
std::atomic
自 C++11 以来是标准的,而 Boost 的东西更旧。但既然它现在是标准的,我更喜欢std::atomic
.- ?? 您可以
std::atomic
在您想要的每个平台上使用每个 C++11 编译器。 没有任何进一步的信息...
标准::原子;
回答by 01100110
I believe std::atomic
(C++11) and boost.atomic
are equivalent. If std::atomic
is not supported by your compiler yet, use boost::atomic
.
我相信std::atomic
(C++11) 并且boost.atomic
是等价的。如果std::atomic
您的编译器尚不支持,请使用boost::atomic
.