C++ Logo

std-discussion

Advanced search

Re: Should std::atomic<T*> have operator->?

From: Thiago Macieira <thiago_at_[hidden]>
Date: Mon, 21 Oct 2019 08:36:53 -0700
On Monday, 21 October 2019 00:44:24 PDT Jeremy Ong via Std-Discussion wrote:
> You have now done an atomic load 3 times and it is possible that stores or
> loads to foo have occurred in between. The pattern for atomic usage is to
> typically load once, perform a change, and possibly compare/swap in a loop
> in a manner that prevents ABA bugs.

If the compiler can inline those three function calls, it may also coalesce
the three loads into one. AFAIK, no compiler currently does it, but it could.
If it can't inline, then the regular rules of visibility apply and if the
atomic pointer's own address is not accessible by other threads and the called
functions, coalescing can happen again.

But if it can change, then the code is actually optimal, since you do have tot
reload it every time. Except that you have to write:

foo.load()->do_one_thing();
foo.load()->do_another_thing();
foo.load()->do_a_final_thing();

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel System Software Products

Received on 2019-10-21 10:39:14