🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Fwd: futexes vs. system semaphores

#1104
From: Chris M. Thomasson
Date: Thu, 03 Dec 2026 21:39
41 lines
2456 bytes
On 12/2/2026 11:09 PM, Bonita Montero wrote:
> I extended my benchmark to test futexes not only with two threads
> but with two threads up to hardware_concurrency() threads. Under
> Linux the performance with three threads up to hc is exactly the
> same as with a Pthread semaphore. Under Windows the performance
> drops to 50% the performance of futexes with three threads and
> above.
> 

You should measure how many times the fast path gets hit:

void FtxSema::acquire()
{
     for( int ftx = m_ftx; ; )
         if( ftx )
             if( m_ftx.compare_exchange_strong( ftx, 0 ) )
                 return;
             else
                 continue;
         else
         {
             m_ftx.wait( 0 );
             ftx = m_ftx;
         }
}


Also, you need to try to fast path the release:

void FtxSema::release()
{
     m_ftx = 1;
     m_ftx.notify_one();
}


Your futex sema is not all that good.

Also, add this to your test bed:

https://vorbrodt.blog/2019/02/05/fast-semaphore/
Technical Details
Message-ID: <11esnho$s9c7$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-ams.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org> <11duq79$vfpt$1@raubtier-asyl.eternal-september.org> <11er4jp$10h0$1@raubtier-asyl.eternal-september.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-ams.pugleaf.net!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!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: Fwd: futexes vs. system semaphores
Date: Thu, 3 Dec 2026 13:39:03 -0800
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <11esnho$s9c7$2@dont-email.me>
References: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org>
 <11duq79$vfpt$1@raubtier-asyl.eternal-september.org>
 <11er4jp$10h0$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 03 Dec 2026 21:39:04 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="927111"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+Cgc78wuYDk1Lup/1cK0LqUP2gm+N6uLg="; posting-host="27be630b90107ef0dd0e5ccb8f536ba4"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:wvEgBCmql3+nuJmJlcMj2GjIw9c= sha256:EDCNACOfLDC2KpmSZcsYyeI0hwnk/zmuEKf7rUcv8Uc=
	sha1:2jiL+pmamq47+34eGNZFzjhdAP0= sha256:MHNiUK87WXDuYjb31WLd/O/UDRAsXSyAewI3IcoFOCM=
Content-Language: en-US
In-Reply-To: <11er4jp$10h0$1@raubtier-asyl.eternal-september.org>