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 8888123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:58:04+00:00 2026-06-14T21:58:04+00:00

I have been carefully studying the accepted answer to the following SO question: C++0x

  • 0

I have been carefully studying the accepted answer to the following SO question: C++0x has no semaphores? How to synchronize threads?

In the semaphore implementation in that answer, here is the implementation of the wait() function:

void wait()
{
    boost::mutex::scoped_lock lock(mutex_);
    while(!count_)
        condition_.wait(lock);
    --count_;
}

I am trying to understand the purpose of the while(!count_) condition.

The answer to another SO question ( How does this implementation of semaphore work? ) indicates that when notify_one() is called on the condition variable, that it is possible that MORE THAN ONE thread waiting on that condition variable will be woken up – hence the need for the while loop. I would like to have this confirmed – is that the full and/or correct answer, or are there other reasons why the while loop is necessary?

If more than one thread wakes up, which thread owns the mutex? The more I think about it, the more ill-defined it seems to be if more than one thread can wake up due to a single call to notify_one(). Would it not be possible for BOTH woken-up threads to see the count_ value as higher than 0, and proceed to both decrement count_, resulting in a count_ value of less than 0, and defeating the purpose (and correctness) of the semaphore?

  • 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-06-14T21:58:05+00:00Added an answer on June 14, 2026 at 9:58 pm

    There could be spurious wakeups, or notify_one could wake up more than one thread due to implementation details, as you’ve already mentioned.

    Waking up multiple threads doesn’t mean though that all of them can enter the protected section at the same time, it just means that when ThreadA releases the lock, ThreadB (which got woken up along with ThreadA in the previous example) also gets to enter the protected section. By this time ThreadA has already done its work, so ThreadB won’t see the count variable in the same state as ThreadA found it.

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

Sidebar

Related Questions

I have been working on an iPhone app that has a feature of uploading
It has been explained to me that if you want to have Artifactory manage
Have been trying to encrypt an xml file to a string so that I
Have been working on this question for a couple hours and have come close
I've been following the Searchable Dictionary example pretty carefully as I'm trying to implement
In a recent interview one peculiar question has been asked a[]= { 1,2,3,4,5,6,7,8,9,10} When
I am working on a short java assignment that I have been set. The
Introduction first, question at the end. Please read carefully! I have a master-detail relation
http://jung.sourceforge.net/ Can someone walk through the steps carefully for me? I have been trying
I know this question has been asked so many times before but I couldn't

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.