• futexes vs. system semaphores

    From Bonita Montero@3:633/10 to All on Sunday, November 22, 2026 12:09:30
    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;

    }

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)
  • From Bonita Montero@3:633/10 to All on Sunday, November 22, 2026 14:20:43
    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;

    }

    --- PyGate Linux v1.5.19
    * Origin: Dragon's Lair, PyGate NNTP<>Fido Gate (3:633/10)