什么时候我真的需要使用atomic< bool>而不是bool? [英] When do I really need to use atomic<bool> instead of bool?
问题描述
不是 atomic< bool>
做了一些冗余的工作?我不认为有一个部分修改bool值。那么bool原子性质?我什么时候真的需要使用 atomic< bool>
而不是bool?
Isn't atomic<bool>
did some redundant work? I don't think there is a partial modified bool value. So is bool atomic by nature? When do I really need to use atomic<bool>
instead of bool?
推荐答案
除非是 std :: atomic *
-something,否则C ++中的
否类型是
No type in C++ is "atomic by nature" unless it is an std::atomic*
-something. That's because the standard says so.
在实际操作中,发出的操作 std :: atomic< bool>
可能(或可能不)与普通的 bool
相同,但原子是一个更大的概念,具有更广泛的分支(例如对编译器的限制 - )。此外,一些操作(如否定)在原子操作上重载,以在硬件上创建与非原子变量的本地非原子读 - 修改 - 写入序列明显不同的指令。
In practice, the actual hardware instructions that are emitted to manipulate an std::atomic<bool>
may (or may not) be the same as those for an ordinary bool
, but being atomic is a larger concept with wider ramifications (e.g. restrictions on compiler re-ordering). Furthermore, some operations (like negation) are overloaded on the atomic operation to create a distinctly different instruction on the hardware than the native, non-atomic read-modify-write sequence of a non-atomic variable.
这篇关于什么时候我真的需要使用atomic< bool>而不是bool?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!