Thread View: comp.lang.c
3 messages
3 total messages
Started by Bonita Montero
Sun, 22 Nov 2026 11:09
futexes vs. system semaphores
Author: Bonita Montero
Date: Sun, 22 Nov 2026 11:09
Date: Sun, 22 Nov 2026 11:09
130 lines
3748 bytes
3748 bytes
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;
}
Fwd: futexes vs. system semaphores
Author: 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;
}
Re: futexes vs. system semaphores
Author: Lawrence D’Oliveiro
Date: Sun, 22 Nov 2026 21:55
Date: Sun, 22 Nov 2026 21:55
9 lines
1596 bytes
1596 bytes
On Sun, 22 Nov 2026 12:09:30 +0100, Bonita Montero wrote: > 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. Which is probably why the Linux kernel offers futexes as a low-level synchronization primitive <https://manpages.debian.org/futex(7)>.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads