Article View: comp.lang.c++
Article #842Re: How to write a semaphore with futexes
From: Ross Finlayson
Date: Fri, 13 Nov 2026 01:37
Date: Fri, 13 Nov 2026 01:37
128 lines
6928 bytes
6928 bytes
On 11/12/2026 01:40 PM, Ross Finlayson wrote: > 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. > > > I figure it can be a POSIX pipe (this is the target) with depth 1, non-blocking on the put side and ignoring errors, blocking on the get side and shifting out of park, producers push to the queue and push to the pipe, consumer pops the queue then parks on the pipe, pretty simple. Then another one is an SPSC queue, and an array of those, say 1000-wide, a queue of PAGE_SIZE buffers with perhaps a tag of timestamp, reading those from the network then decrypting and decompressing them then incremental-parsing them, then, to pop those off, yet, that's the same thread on both sides so it doesn't need any synchronization at all. These days it's all the rage this "io_uring" since "epoll", idea seeming to be "let's get these naughty syscalls out of our code and just shove it right in the kernel, now we're blazing fast", and, it's like, that's squeezing the balloon. And it's like, "so, what happens when limits that b.t.w. the kernel doesn't advertise" and it's like, "oh, they just get dropped, that's a kernel problem not our code". Hmm....
Technical Details
Message-ID:
<otOdnX9XeuBl9Gv3nZ2dnZfqnPSdnZ2d@giganews.com>Path:
mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.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> <11d566l$8mlf$1@dont-email.me> <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-1.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-4.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 13 Nov 2026 01:38:00 +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> <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Thu, 12 Nov 2026 17:37:51 -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: <-KucnWlDQ9Whr2v3nZ2dnZfqnPqdnZ2d@giganews.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <otOdnX9XeuBl9Gv3nZ2dnZfqnPSdnZ2d@giganews.com> Lines: 128 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-QftJfBUnaoR2uz2qwtSPAfSLfetMnd36GxhSlhu+LnBoF3KXZyMUuiwxTZGLu/D0EifvZQUuDtb9hlT!Z+1PN6BUw/ggc/uIR5kgajJLHOJqaYTLsZg1Lsp7/rQXaGuIk+gHTyqKhzcE21biCwXis2zlZrsj 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