I'm having trouble wrapping my head around the correct usage of std::atomic_ref<int>
with volatile
.
Naively there are three possibilities:
std::atomic_ref<volatile int> ref1;
volatile std::atomic_ref<int> ref2;
volatile std::atomic_ref<volatile int> ref3;
When and do we want to use each one? The use-case I'm interested in is MMIO.
Unlike
std::atomic<T>
,std::atomic_ref<T>
does not havevolatile
-qualified methods. So, you probably can't do much with avolatile std::atomic_ref<T>
(whether T is itself volatile or not).This makes sense given the quote
Assuming cv-qualification is somewhat consistent, a shallowly-volatile atomic_ref is unlikely to be useful, and definitely isn't what you're asking for.
So, you want
Note that it may be sufficient to just use
std::atomic_ref<int>
, but since the standard doesn't make any explicit guarantees about MMIO, you should probably consult your compiler documentation and/or check the code it generates.Depending on
std::atomic
in this way is at least not portable. Specifically, this answer and its linked paper mention some ways in whichstd::atomic
may be inadequate - you can check whether these are actual problems for you.