Correct usage of volatile with std::atomic_ref<T>

123 views Asked by At

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.

1

There are 1 answers

1
Useless On

Unlike std::atomic<T>, std::atomic_ref<T> does not have volatile-qualified methods. So, you probably can't do much with a volatile std::atomic_ref<T> (whether T is itself volatile or not).

This makes sense given the quote

Like language references, constness is shallow for atomic_ref - it is possible to modify the referenced value through a const atomic_ref object.

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

std::atomic_ref<volatile int>

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 which std::atomic may be inadequate - you can check whether these are actual problems for you.