Article View: comp.lang.c++
Article #986Fwd: futexes vs. system semaphores
From: Bonita Montero
Date: Sun, 22 Nov 2026 13:20
Date: Sun, 22 Nov 2026 13:20
139 lines
4310 bytes
4310 bytes
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;
}
}
int main()
{
constexpr size_t Rounds = 100'000;
auto test = []<typename Sema>( Sema * )
{
Sema semA( 1 ), semB( 0 );
atomic_uint64_t nsSum = 0;
latch lat( 3 );
auto thr = [&]( Sema &semMe, Sema &semYou )
{
lat.arrive_and_wait();
time_point start = steady_clock::now();
for( size_t r = Rounds; r; --r )
{
semMe.acquire();
semYou.release();
}
duration dur = steady_clock::now() - start;
int64_t ns = duration_cast<nanoseconds>( dur ).count();
nsSum += ns;
};
jthread
thrA( thr, ref( semA ), ref( semB ) ),
thrB( thr, ref( semB ), ref( semA ) );
lat.arrive_and_wait();
thrA.join(), thrB.join();
return (double)nsSum / (2 * 1.0e9);
};
double
tFtx = test( (FtxSema *)nullptr ),
tSys = test( (SysSema *)nullptr ),
rel = trunc( 1'000.0 * tSys / tFtx + 0.5 ) / 10.0;
cout << rel << "%" << endl;
}
Technical Details
Message-ID:
<11duq79$vfpt$1@raubtier-asyl.eternal-september.org>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mailReferences:
<11duih8$sm6c$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!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail From: Bonita Montero <Bonita.Montero@gmail.com> Newsgroups: comp.lang.c++,comp.lang.c Subject: Fwd: futexes vs. system semaphores Followup-To: comp.lang.c++ Date: Sun, 22 Nov 2026 14:20:43 +0100 Organization: A noiseless patient Spider Lines: 139 Message-ID: <11duq79$vfpt$1@raubtier-asyl.eternal-september.org> References: <11duih8$sm6c$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: Sun, 22 Nov 2026 13:20:42 +0000 (UTC) Injection-Info: raubtier-asyl.eternal-september.org; logging-data="1031997"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19XdVV0L+f94E8D/2qVGgR3B7YzKtXUNVM="; posting-host="6a8b15f381833ff0e2496b5e89cb040b" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:7LSBLd7rWoGWa4C3S14P+X8Vtfc= sha256:qVFPn2EWCRtVTbYdXNSCD2TN8P6e2Gt8i1eLJQ92R+o= sha1:wcF0OGzpNpkXmtW8owjRacKbgQU= sha256:4I9Vz5RwlSz/h8+8OiOVJrGO5ySFacE6M5GOkqZLMFQ= X-Forwarded-Message-Id: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org> In-Reply-To: <11duih8$sm6c$1@raubtier-asyl.eternal-september.org> Content-Language: de-DE