C++ Logo

std-discussion

Advanced search

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

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 22 Oct 2019 18:14:18 +0300
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
> wrote:
>>> Sometimes it can
>>>
>>> void f()
>>> {
>>>
>>> std::atomic_ptr<MyType> ptr = get_object();
>>> ptr->do_one_thing();
>>> ptr->do_another_thing();
>>>
>>> }
>>
>> 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.

Received on 2019-10-22 10:16:36