C++ Logo


Advanced search

Re: atomic_ref::required_alignment usefulness

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sun, 09 Feb 2020 22:41:58 -0800
On Sunday, 9 February 2020 06:15:44 PST Andrey Semashev via Std-Discussion
> Hi,
> As specified in N4849, atomic_ref<T>::required_alignment is:
> The alignment required for an object to be referenced by an atomic
> reference, which is at least alignof(T).
> This wording does not give provision as to whether this alignment is
> strict enough so that atomic_ref<T> is lock-free (provided that it can
> be lock-free at all). At the same time, atomic_ref<T> does not provide a
> constant for the alignment that guarantees lock-free operations (again,
> provided that they can be lock-free). Let's call this missing constant
> recommended_alignment.
> Is my understanding correct? If so, it seems like required_alignment is
> rather useless, as there seems to be no reason to make it anything other
> than alignof(T). Why is recommended_alignment, which is much more
> useful, missing?

I'm not understanding why you want two constants, instead of just one. Can you
help by giving an example of an architecture where the two constants would be
different. Please specify whether that architecture is real or hypothetical.

The example that comes to me is i386, where alignof(8 bytes) is often 4, but
atomic access requires them to be aligned to 8. That is,
        atomic_ref<long long>::required_alignment > alignof(long long)

PS: the hardware doesn't actually require this alignment, but please shoot any
code that tries to do unaligned atomic access with extreme prejudice.

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

Received on 2020-02-10 00:44:38