🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to write a semaphore with futexes

#861
From: Chris M. Thomasson
Date: Sat, 14 Nov 2026 23:12
64 lines
4242 bytes
On 11/13/2026 6:44 AM, Joseph Seigh wrote:
> On 11/12/26 3: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:
>>>>
>>>>
>>>> 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.
>>
> 
> You don't need to do an XCHG.  You have a single consumer.  It owns the 
> queue.  It just needs to do a load of the queue tail pointer and just
> process the queue.  Of course it needs to keep track of things like the
> last tail pointer value it read so it knows when it has processed all
> the new nodes.  It may also want to defer deallocating the node
> referenced by the tail pointer to avoid the ABA problem.

That should work fine. Well, I need to take another look at:

https://cs.rochester.edu/research/synchronization/pseudocode/queues.html


> You could do the XCHG if you wanted a way for the consumer to indicate
> it was going to block and needed to be woken up.  There are other
> ways that don't need this logic so it's a design choice.

Big time! Eventcounts are fun. Actually, a futex itself might be able to 
be used here.


>>> 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.
> 
> It's unlikely anyone has program logic that requires FIFO, what little
> of it is actually observable.  You really care about forward progress
> guarantees.  As long as the consumer keeps up with the producers, you
> should be ok doing batches in LIFO order.  If not, you have other
> problems.

True.


> And if you drop the FIFO requirement then you could just have multiple
> consumers doing XCHG on the queue tail pointer.

That's ideal.
Technical Details
Message-ID: <11dapt8$2bds2$1@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> <11d77og$tk4f$1@dont-email.me>

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:12:37 -0800
Organization: A noiseless patient Spider
Lines: 64
Message-ID: <11dapt8$2bds2$1@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>
 <11d77og$tk4f$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 14 Nov 2026 23:12:42 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="2471810"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX185W718mMcdxaXbNFnCT8CPXjvHncqiwXA="; posting-host="0bd5635eb0cb5bcacb756ace2510ea48"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:LzLVstBzxM8jHlWK+eC7geGK/xk= sha256:uglg0x88a0tyAz+oHvs8tRNfcMJfr6C3oUyMoZROVsA=
	sha1:T72+DsGDe7/93xQ70a+X89ojEAc= sha256:wixpSClMrN0xaZrQjuBYSFxWkRo/SPBNptbzBylRYf0=
Content-Language: en-US
In-Reply-To: <11d77og$tk4f$1@dont-email.me>