mardi 24 juillet 2018

Why all the member functions in std::atomic appear both with and without volatile?

I noticed that most member functions of std::atomic<T> types are declared twice, once with volatile modifier and once without (example)). I checked the source code of the G++ standard library implementation and I found all of them to be exact duplicates, for example,

bool
load(memory_order __m = memory_order_seq_cst) const noexcept
{ return _M_base.load(__m); }

bool
load(memory_order __m = memory_order_seq_cst) const volatile noexcept
{ return _M_base.load(__m); }

I could not find any example where the volatile variant behaves differently than the non-volatile one, differs in return type or anything of that sort.

Why is that? I thought a volatile member function could also be called in objects which are not volatile. So declaring and defining std::atomic::load(...) const volatile noexcept etc. should be enough.

Aucun commentaire:

Enregistrer un commentaire