🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to write a semaphore with futexes

#849
From: Joseph Seigh
Date: Fri, 13 Nov 2026 14:44
50 lines
3802 bytes
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.

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.


>>
>> 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.

And if you drop the FIFO requirement then you could just have multiple
consumers doing XCHG on the queue tail pointer.
Technical Details
Message-ID: <11d77og$tk4f$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>

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: Joseph Seigh <jseigh_es00@xemaps.com>
Newsgroups: comp.lang.c++
Subject: Re: How to write a semaphore with futexes
Date: Fri, 13 Nov 2026 09:44:25 -0500
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <11d77og$tk4f$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 13 Nov 2026 14:44:33 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="970895"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18vF6X3urmdM2WDtcvOjp+G"; posting-host="811f869b2d619366c5d59c437005a0f4"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:fEF9J6frUmqiH5J6CGEiy8FZvf8= sha256:lR32F0ddFHGsj/bjAJ4mXJRW00Fm/glmK0gnJbbnYBg=
	sha1:CIE1YIZpYV87vWujDeDqXhjWFBI= sha256:SZ38gDb76fGs0bFQDQncfBYStxkUAupF9ZbmHHru698=
Content-Language: en-US
In-Reply-To: <11d566l$8mlf$1@dont-email.me>