🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: How to write a semaphore with futexes

#838
From: Ross Finlayson
Date: Thu, 12 Nov 2026 21:40
101 lines
5652 bytes
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.


Technical Details
Message-ID: <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.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!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!news.nrbbs.org!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 12 Nov 2026 21:40:11 +0000
Subject: Re: How to write a semaphore with futexes
Newsgroups: comp.lang.c++
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>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Thu, 12 Nov 2026 13:40:03 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11d566l$8mlf$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Message-ID: <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>
Lines: 101
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-uVx05vJyQlcmJfH8/e3KQyMpeSYTe9+f24NmBAgxLzVL5/21i2JjrrPdvzOYHhK4Sbu6hTD7lgx5IPc!AW5ZlMq30Vgq/jaKWmWiESwAQlVXi7XY43NKNW6p6kNsx2R1+XBx9KUmhcWWGm7JzGSTDz5FugD+
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40