Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7526681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:53:58+00:00 2026-05-30T03:53:58+00:00

First of all, I know that it can be implemented with a mutex and

  • 0

First of all, I know that it can be implemented with a mutex and condition variable, but I want the most efficient implementation possible.
I would like a semaphore with a fast-path when there’s no contention. On Linux this is easy with a futex; for example, here’s a wait:

if (AtomicDecremenIfPositive(_counter) > 0) return; // Uncontended
AtomicAdd(&_waiters, 1);
do
{
    if (syscall(SYS_futex, &_counter, FUTEX_WAIT_PRIVATE, 0, nullptr, nullptr, 0) == -1) // Sleep
    {
        AtomicAdd(&_waiters, -1);
        throw std::runtime_error("Failed to wait for futex");
    }
}
while (AtomicDecrementIfPositive(_counter) <= 0);
AtomicAdd(&_waiters, -1);

and post:

AtomicAdd(&_counter, 1);
if (Load(_waiters) > 0 && syscall(SYS_futex, &_counter, FUTEX_WAKE_PRIVATE, 1, nullptr, nullptr, 0) == -1) throw std::runtime_error("Failed to wake futex"); // Wake one

At first I thought for Windows to just use NtWaitForKeyedEvent(). The problem is it’s not a direct substitution because it doesn’t atomically check the value at _counter before going into the kernel, and so can miss the wake from NtReleaseKeyedEvent(). Worse, then NtReleaseKeyedEvent() would block.
What’s the best solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:53:59+00:00Added an answer on May 30, 2026 at 3:53 am

    I think something like this should work:

    // bottom 16 bits: post count
    // top 16 bits: wait count
    struct Semaphore { unsigned val; }
    
    wait(struct Semaphore *s)
    {
    retry:
        do
            old = s->val;
            if old had posts (bottom 16 bits != 0)
                new = old - 1
                wait = false
            else
                new = old + 65536
                wait = true
        until successful CAS of &s->val from old to new
    
        if wait == true
            wait on keyed event
            goto retry;
    }
    
    post(struct Semaphore *s)
    {
        do
            old = s->val;
            if old had waiters (top 16 bits != 0)
                // perhaps new = old - 65536 and remove the "goto retry" above?
                // not sure, but this is safer...
                new = old - 65536 + 1
                release = true
            else
                new = old + 1
                release = false
        until successful CAS of &s->val from old to new
    
        if release == true
            release keyed event
    }
    

    edit: that said, I’m not sure this would help you a lot. Your thread pool usually should be big enough that a thread is always ready to process your request. This means that not only waits, but also posts will always take the slow path and go to the kernel. So, counting semaphores are probably the one primitive where you do not really care about a userspace-only fastpath. Stock Win32 semaphores should be good enough. That said, I’m happy to be proven wrong!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, I know that UDP is not a reliable protocol but I
First of all: I do know that there are already many questions and answers
First of all I know similar questions has been asked here, I've checked but
First of all I don't know if this is the right approach. I want
First of all sorry for the name of the title, but i dont know
all I implemented my first PowerShell script, that does some setup, sets registry keys
I implemented paging for one of my GridViews. Now I want that all checked
First of all i don't know whether the title is relevant or not (please
first of all i would like to say i know its probably an easy
First of all, I know there are a few quite similar questions here on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.