To summarize what others have correctly written:
C/C++ volatile
is for hardware access and interrupts. C++11 atomic<>
is for inter-thread communication (e.g., in lock-free code). Those two concepts/uses are orthogonal, but they have overlapping requirements and that is why people have often confused the two.
The reason that atomic<>
has volatile-qualified functions is the same reason it has const-qualified functions, because it's possible in principle for an object be both atomic<>
and also const
and/or volatile
.
Of course, as my article pointed out, a further source of confusion is that C/C++ volatile
isn't the same as C#/Java volatile
(the latter is basically equivalent to C++11 atomic<>
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…