Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

atomic operation with volatile atomic variable

Tags:

c++

c++11

atomic

Why c++ atomic operations has an overloaded version for volatile atomic<T>?

When are we required declare atomic<T> as volatile and what is difference between atomic<T> and volatile atomic<T>?

like image 713
MRB Avatar asked Oct 02 '22 18:10

MRB


1 Answers

It's the same as with any other type: you need to volatile-qualify your atomic if you're performing atomic operations on a memory-mapped I/O register or otherwise require the semantics of volatile-qualified types (which are not related in any way to atomicity or to the inter-thread synchronization and memory ordering provided by atomic operations).

The standard has this to say about the volatile overloads for atomics (29.6.5[atomics.types.operations.req]/3)

[ Note: Many operations are volatile-qualified. The “volatile as device register” semantics have not changed in the standard. This qualification means that volatility is preserved when applying these operations to volatile objects. It does not mean that operations on non-volatile objects become volatile. Thus, volatile qualified operations on non-volatile objects may be merged under some conditions. —end note ]

like image 81
Cubbi Avatar answered Oct 13 '22 10:10

Cubbi