C++ Logo

std-discussion

Advanced search

Re: Synchronization of atomic notify and wait

From: Thiago Macieira <thiago_at_[hidden]>
Date: Tue, 26 Jul 2022 09:01:56 -0700
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?

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.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel DPG Cloud Engineering

Received on 2022-07-26 16:01:58