Article View: comp.lang.c++
Article #837Re: How to write a semaphore with futexes
From: Chris M. Thomasson
Date: Thu, 12 Nov 2026 20:05
Date: Thu, 12 Nov 2026 20:05
73 lines
4307 bytes
4307 bytes
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. >> >> >> >> > >
Technical Details
Message-ID:
<11d566l$8mlf$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-mailReferences:
<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>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 12:05:37 -0800 Organization: A noiseless patient Spider Lines: 73 Message-ID: <11d566l$8mlf$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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 12 Nov 2026 20:05:41 +0000 (UTC) Injection-Info: dont-email.me; logging-data="285359"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/xrlXvo7iwqQEsQbPWp194Oh/ZmnaKEnA="; posting-host="93656593b0eb380777096da46e3e6c37" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:i4rEyuQj6PI8/5fe8PiLExuCLd8= sha256:886qRrWQPCnLeUvY8k+Jeevgz6F6ZqfbuA8K8lQpnHw= sha1:fyeDb2eY852NLR6Xdl8i7rAK3Z0= sha256:nT49FyY+ZR9+Q+5Bro1EQrR565aVW5PCaM06deE837I= Content-Language: en-US In-Reply-To: <11d55rp$8hhb$1@dont-email.me>