🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to write a semaphore with futexes

#835
From: Chris M. Thomasson
Date: Thu, 12 Nov 2026 19:59
62 lines
3788 bytes
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.

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

Technical Details
Message-ID: <11d55rp$8hhb$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>

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: Thu, 12 Nov 2026 11:59:48 -0800
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <11d55rp$8hhb$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>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 12 Nov 2026 19:59:54 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="280107"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18V9owKldgs3qaOhVZZWUD41byaWgRw8bQ="; posting-host="93656593b0eb380777096da46e3e6c37"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1oMT2PugZHPd6V3V0DXPK9qTelc= sha256:zO3aqQxDv6mcVL5U0fGDvqUbAqbgPJTrhqvK+xU4WB8=
	sha1:rBQJgYxYz/whXh0ow5Mw3PmqoRc= sha256:irCQ7TgmrUdPteSR7QQ2bi6HM4/yWW1/JkgvGVYwX9Y=
In-Reply-To: <0iGdnbMEEbSZiGv3nZ2dnZfqnPqdnZ2d@giganews.com>
Content-Language: en-US