When do I really need to use atomic<bool> instead of bool?
atomic, c++, c++11, stdatomic
Solution
No type in C++ is "atomic by nature" unless it is an `std::atomic*`-something. That's because the standard says so.
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.
Problem
Isn't `atomic<bool>` redundant because `bool` is atomic by nature? I don't think it's possible to have a partially modified bool value. When do I really need to use `atomic<bool>` instead of `bool`?
Related problems
- Do I have to use atomic<bool> for "exit" bool variable?
- Why is sizeof(bool) not defined to be one by the C++ standard?
- Can a bool read/write operation be not atomic on x86?
- What's the difference of the usage of volatile between C/C++ and C#/Java?
- Do I have to use atomic<bool> for "exit" bool variable?
- Can a bool read/write operation be not atomic on x86?