🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c++
Article #863

Re: How to write a semaphore with futexes

#863
From: Chris M. Thomasson
Date: Sat, 14 Nov 2026 23:18
106 lines
5815 bytes
On 11/12/2026 1:40 PM, Ross Finlayson wrote:
> On 11/12/2026 12:05 PM, Chris M. Thomasson wrote:
>> On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
>>> On 11/12/2026 11:35 AM, Ross Finlayson wrote:
>>>> On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
>>>>> On Wed, 11 Nov 2026 15:05:43 -0800
>>>>> "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
>>>>>> On 11/11/2026 2:47 PM, Bonita Montero wrote:
>>>>>>> Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
>>>>>>>
>>>>>>>> I see. A fun failure on a sem_wait is EINTR.
>>>>>>>
>>>>>>> That's not a out of recources error and it is recoverable.
>>>>>>
>>>>>> Its scary though when people fail to know about it.
>>>>>
>>>>> Anyone who doesn't read the man page before they use an unfamiliar
>>>>> function
>>>>> frankly deserves what they get.
>>>>>
>>>>
>>>>
>>>> Here's one for you.
>>>>
>>>> So, I want a MPSC queue, multiple-producer single-consumer queue,
>>>> producers on the order of 1000, with that appends to the queue push(),
>>>
>>> A 1000? You have a 1000 cores handy? ;^)
>>>
>>> Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
>>> single XCHG for the pop. Adding multiple nodes is easy. push the head
>>> and tail.
>>
>> I should clarify that a single XCHG for the pop is basically flushing
>> the whole list. But its easy. So, if that works for you fine. Now, this
>> can get almost "too" much work for a thread that pops a huge number of
>> nodes. But, we can distribute the MPSC to try to balance the load. Using
>> a single XCHG for the single consumer is basically the best we can get.
>>
>>
>>
>>>
>>> Now. For a FIFO reversing the XCHG, done. Now, there is the classic
>>> MPMC lock free queue. Iirc, I think there is a way to specialize it on
>>> the single consumer case.
>>>
>>> To make this be able to wait, ahh... The wonderful eventcount comes
>>> into play here. It can be created with a futex or other means. Its
>>> basically a "condition variable" for lock/wait-free.
>>>
>>> Have you ever used them before?
>>>
>>>
>>>
>>>> push_back, pretty much must use compare-and-swap the only atomic
>>>> primitive on the chip, though for example with having a buffer and 
>>>> using
>>>> conditional-move after compare-and-swap to copy items into the buffer
>>>> then appending multiple items from the buffer atomically. Then, the
>>>> consumer, should have a non-blocking peek() and pop(), peek_front and
>>>> pop_front, that should return an error if EWOULDBLOCK, then that it can
>>>> park in a syscall when the queue's empty, so that it idles in park with
>>>> 0% CPU. Then pushing items onto the queue should change it back to
>>>> non-blocking.
>>>>
>>>> So, when it's running it should be un-blocked, and appends must be
>>>> non-blocking, when it's empty, then it should block to "park".
>>>>
>>>> In C and C++ and in POSIX, how's this.
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>
> 
> 
> Hm. There are 1000 "logical" producers, subjects, say, of a topic,
> from 3 or 4 threads, and it's usually figured that these will all
> be pinned to one core, since the idea is that the entire process they're
> in has thusly affinity of their memory, and that the process-group
> scales in processes about kernel resources per-process
> and these kinds of things.
> 
> It's figured that items are 64B or 128B, i.e., cache-lines or
> cache-lines-pairs size, and that the heap backing the queue is of 4KiB
> i.e. PAGE_SIZE size pages. Or, the items may be larger, yet, fixed-size.
> 
> 
> It's only a FIFO, so, there's only push (push_back) and pop (pop_front)
> relevant.
> 
> The queue may grow or is unbounded, bounded by RAM, in user-space,
> it's not a ring buffer.
> 
> When there's activity on the system, the queue will likely be non-empty,
> yet when the activity is low, it can go in and out of idle.
> When the activity is idle, it shall block in park and not be scheduled
> by the CPU, the single thread that consumes it.
> 
> 
> 

An eventcount would work for you there. Or perhaps a raw futex, that is 
more low level.
Technical Details
Message-ID: <11daq8s$2bds2$3@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11cvher$2ahke$1@raubtier-asyl.eternal-september.org> <11d02gm$2h8g0$1@dont-email.me> <11d145g$2qlhs$1@raubtier-asyl.eternal-september.org> <11d2p9c$3eke4$1@dont-email.me> <11d2r97$3f9sa$2@raubtier-asyl.eternal-september.org> <11d2sca$3fn5d$1@dont-email.me> <11d4q72$bdct$1@dont-email.me> <0iGdnbMEEbSZiGv3nZ2dnZfqnPqdnZ2d@giganews.com> <11d55rp$8hhb$1@dont-email.me> <11d566l$8mlf$1@dont-email.me> <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups: comp.lang.c++
Subject: Re: How to write a semaphore with futexes
Date: Sat, 14 Nov 2026 15:18:52 -0800
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <11daq8s$2bds2$3@dont-email.me>
References: <11cvher$2ahke$1@raubtier-asyl.eternal-september.org>
 <11d02gm$2h8g0$1@dont-email.me>
 <11d145g$2qlhs$1@raubtier-asyl.eternal-september.org>
 <11d2p9c$3eke4$1@dont-email.me>
 <11d2r97$3f9sa$2@raubtier-asyl.eternal-september.org>
 <11d2sca$3fn5d$1@dont-email.me> <11d4q72$bdct$1@dont-email.me>
 <0iGdnbMEEbSZiGv3nZ2dnZfqnPqdnZ2d@giganews.com>
 <11d55rp$8hhb$1@dont-email.me> <11d566l$8mlf$1@dont-email.me>
 <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 14 Nov 2026 23:18:53 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2471810"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+3vzVpcPlmPB/8cwfXQzr/F/AxWB4gXz0="; posting-host="0bd5635eb0cb5bcacb756ace2510ea48"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:v/VGnCqreoE1nZAaBhgl0iztN70= sha256:SOrP4phNevhAyo3WQYPvYUnVf0psCyvu9bh4ITnJgfE=
	sha1:6lSVybiZIFRtgRIG0vamfBD11M8= sha256:IW3vUNcn9m1VURRGsimjKg+8G/TJO3DcnOG21Yf2vBQ=
Content-Language: en-US
In-Reply-To: <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>