C++ Logo

std-discussion

Advanced search

Re: Synchronization of atomic notify and wait

From: Andrey Semashev <andrey.semashev_at_[hidden]>
Date: Tue, 26 Jul 2022 20:08:15 +0300
On 7/26/22 19:01, Thiago Macieira via Std-Discussion wrote:
> On Tuesday, 26 July 2022 08:52:07 PDT Andrey Semashev via Std-Discussion
> wrote:
>> The point is to use futex even with atomics smaller than 32-bit because
>> this is arguably more efficient than a lock/futex pool. But given that
>> there is a tradeoff of performing all atomic operations using
>> masking+CAS, and that wait/notify is likely the minority of atomics
>> usage, it probably isn't worth it in a general case such as std::atomic.
>> But it may be worth doing in other, more specialized cases.
>
> Not necessarily. You could use 8- and 16-bit atomics in userspace and simply
> do a 32-bit futex on the word containing those half- and quarter-word atomics.
> The kernel does not actually do atomics inside the futex code because it needs
> far more code than is possible in atomic instruction, or even in an LL/SC
> section.
>
> However, using this technique is now depending on undocumented kernel
> behaviour. It probably works, but is it worth the risk?

Futex itself is not exactly documented, so we're already poking the
kernel where normal people wouldn't. :)

> Anyway, right now, because of implementation constraints, you only get efficient
> atomic waits on Linux with int, and you don't get efficient waits outside of
> Linux with any type. If this is important to your code base, then skip
> atomic_wait and go straight to the OS API for futexes and semaphores.

Futex-like functionality is actually quite wide spread. Windows, various
BSDs and MacOS/iOS/etc. also provide similar APIs. Having a portable API
for it is useful. (Though I don't necessarily agree this API should be
std::atomic, but that's a different matter.)

Received on 2022-07-26 17:08:20