Subject: Re: Should std::atomic<T*> have operator->?
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2019-10-22 10:14:18
On 2019-10-22 18:00, Thiago Macieira via Std-Discussion wrote:
> On Tuesday, 22 October 2019 00:14:52 PDT Andrey Semashev via Std-Discussion
>>> Sometimes it can
>>> void f()
>>> std::atomic_ptr<MyType> ptr = get_object();
>> In such pathologic cases I question whether atomic is needed in the
>> first place.
> You're probably right, but it's easy to see how it would happen if the atomic
> is a member of a class.
Not really. A member of a class can be accessed from multiple places,
including via different pointers and references. Even if you only access
it from a single method of the class, that method can be called
concurrently. I don't see how a compiler would be able to prove that
that doesn't happen, ever. And if that indeed clearly doesn't happen
then that is probably another pathological case where atomic is not needed.
Atomics are used to synchronize threads (obviously), so when an atomic
is used, that by itself is a clear sign that concurrency is in place.
STD-DISCUSSION list run by herb.sutter at gmail.com
Older Archives on Google Groups