-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Volatile might not be the best (or even right) implementation for atomics #4700
Description
As discussed in #4583 this is a separate issue and needs its own thread. The original source for this consideration was a problem with static initialization of structs containing non-literal members (e.g., ones with volatile) in C++.
However, during the following discussion it was questioned if volatile is the right way to implement atomic integers. Here are some of our sources:
- GCC doc
- Linux kernel guidelines on atomics
- Blog article on volatile in embedded systems
- Paper on volatile in nesC
And some posts on Stack Overflow:
And related Wikipedia entires:
My current understanding is that memory barriers would be preferable to volatile. However, there are different barriers (compiler and hardware), which prevent memory reordering at different points. ARM architectures seem to have different reordering strategies than x86 ones.
Hardware memory barriers:
Two questions:
- Would an implementation without
volatilebe preferable? - What would it look like?