🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Fwd: futexes vs. system semaphores

#1043
From: Chris M. Thomasson
Date: Sun, 29 Nov 2026 02:58
107 lines
3981 bytes
On 11/22/2026 5:20 AM, Bonita Montero wrote:
> Sorry, this should to to comp.lang.c++
> 
> -------- Weitergeleitete Nachricht --------
> Betreff: futexes vs. system semaphores
> Datum: Sun, 22 Nov 2026 12:09:30 +0100
> Von: Bonita Montero <Bonita.Montero@gmail.com>
> Organisation: A noiseless patient Spider
> Newsgruppen: comp.lang.c
> 
> I developed a little Win32 / Posix C++20-application that benchmarks
> futexes vs. system semaphores. I expected a significant advantage of
> futexes, but not that: With Win32 futexes are 11.6 times faster than
> semaphores, with WSL2 they're 10 times faster.
> Please show me your numbers.
> 
> #if defined(_WIN32)
>      #include <Windows.h>
> #elif defined(__unix__)
>      #include <pthread.h>
> #endif
> #include <iostream>
> #include <thread>
> #include <chrono>
> #include <atomic>
> #include <latch>
> #include <cmath>
> 
> using namespace std;
> using namespace chrono;
> 
> struct SysSema
> {
>      SysSema( bool initial );
>      void release();
>      void acquire();
> #if defined(_WIN32)
>      HANDLE m_hEvt = NULL;
> #elif defined(__unix__)
>      sem_t m_sem;
> #endif
> };
> 
> SysSema::SysSema( bool initial )
> #if defined(_WIN32)
>      : m_hEvt( CreateEvent( nullptr, FALSE, (BOOL)initial, nullptr ) )
> #endif
> {
> #if defined(__unix__)
>      sem_init( &m_sem, 0, (int)initial );
> #endif
> }
> 
> void SysSema::release()
> {
> #if defined(_WIN32)
>      SetEvent( m_hEvt );
> #elif defined(__unix__)
>      sem_post( &m_sem );
> #endif
> }
> 
> void SysSema::acquire()
> {
> #if defined(_WIN32)
>      WaitForSingleObject( m_hEvt, INFINITE );
> #elif defined(__unix__)
>      sem_wait( &m_sem );
> #endif
> }
> 
> struct FtxSema
> {
>      FtxSema( bool initial );
>      void release();
>      void acquire();
>      atomic_int m_ftx;
> };
> 
> FtxSema::FtxSema( bool initial ) :
>      m_ftx( (int)initial )
> {
> }
> 
> void FtxSema::release()
> {
>      m_ftx = 1;
>      m_ftx.notify_one();
> }
> 
> 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;
>          }
> }

Notice how your futex logic has a fast path? a kernel semaphore does not 
have any of that.
Technical Details
Message-ID: <11eg4cv$35i6g$2@dont-email.me>
Path: mbox-mo1.pugleaf.net!srv.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.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: Sat, 28 Nov 2026 18:58:38 -0800
Organization: A noiseless patient Spider
Lines: 107
Message-ID: <11eg4cv$35i6g$2@dont-email.me>
References: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org>
 <11duq79$vfpt$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 29 Nov 2026 02:58:40 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3328208"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/2Z5InlE8mrHOrVsou8D6qMaTuOHdhGS8="; posting-host="154a170364bb3f201858dda21f806e24"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:XywewsTMF8xaDxhIaCKJpsvAEVk= sha256:kWrJbr4FJRCyWlFN9DMzvy82L6sfp6mhcMsLf4x7D5U=
	sha1:x9/z76JdiWk28AxfC9DL9d6GHrY= sha256:aHAliLtBqSmhclNiZOO4SFqYxkIBp2eeNnnhXzivciI=
Content-Language: en-US
In-Reply-To: <11duq79$vfpt$1@raubtier-asyl.eternal-september.org>