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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:52:09+00:00 2026-06-13T23:52:09+00:00

The title basically speaks for itself. If I make a call to pthread_cond_wait with

  • 0

The title basically speaks for itself. If I make a call to pthread_cond_wait with a particular condition and mutex, will that thread stay blocked until a call to pthread_cond_signal is made with the corresponding condition? Or will it unblock regardless if the mutex is subsequently unlocked again?

If the answer is the former, I have a follow-up. I have a queue used for message-passing between my threads. I want to ensure only one thread can append an item to the queue at a time (hence the use of mutexes). None of the threads have any idea if any other thread will be waiting to take hold of the mutex themselves.

When attempting to append an item to the queue, I lock the mutex, wait for the queue to not be full with a pthread condition, then perform the append, then unlock the mutex. Before unlocking it, should I perform a pthread_cond_signal, even if I don’t know whether or not any other threads will be waiting? What happens if multiple threads are waiting?

  • 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-13T23:52:10+00:00Added an answer on June 13, 2026 at 11:52 pm
    1. Someone needs to signal/broadcast the condition variable, otherwise waiting threads don’t necessarily get woken up. You can also get “spurious” wake-ups, but don’t rely on them. Once a waiter has been woken (by a signal or broadcast or spuriously), it will start trying to acquire the mutex, and once it has that pthread_cond_wait returns.

    2. If there are multiple threads waiting then one of them is woken (arbitrarily or according to rules documented by the implementation). In your code as you describe it, the only place a thread ever waits on this condvar is when it’s trying to add an item to a full queue. So you have three options:

      • signal the condition variable each time you remove an element from a full queue. This wakes one of the 0 or more waiters. Then signal again after it adds an element, if the queue is still not full, just in case there was more than 1 waiter.

      • broadcast the condition variable each time you remove an element from a full queue. Now you don’t need to signal after adding an element.

      • signal the condition variable each time you remove an element, regardless of whether the queue is full or not. If you do this, don’t make the mistake of changing the code in future to remove two elements in one go and only signal once. Condition variables reduce the number of waiters by one each time you signal, and this approach works because it prevents there ever simultaneously being at least one waiter, and fewer woken ex-waiters trying to acquire the mutex than there are spaces in the queue. So you never get threads waiting when there’s a space they could use.

    The way to think of this is that the “condition” you’re waiting for is, “the queue is not full”. Whenever the condition becomes true, you should signal or broadcast the condition variable. If you choose to signal, and there might be multiple waiters, then each waiter needs to signal (to wake the next waiter) if the condition remains true after it has done its thing.

    If you don’t care about the performance cost of a few extra context switches, and provided you have written the code that waits on the condvar correctly to cope with spurious wakes, it is also safe to signal or broadcast a condvar when the condition is still false. This is one of the nice things about condition variables, it makes it easier to reason about the correctness of the code. So the requirement “whenever the condition becomes true, you should signal or broadcast the condition variable” means only what it says. You don’t have to signal if and only if the condition becomes true.

    So in this case it would be fine to signal each time you remove an element from the queue, regardless of whether or not it was full beforehand. In this case it’s easy to cut down the number of signals to the minimum necessary, but occasionally it is more trouble than it is worth to work out for sure whether the condition has become true, so instead you signal if it’s now true and might have been false before, or even just signal if it might be true now. I wouldn’t advise putting in extra wakes for no reason, but sometimes it makes the code simpler.

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

Sidebar

Related Questions

Basically what the title is saying. I'm trying to make a small app that
Basically the title speaks for itself. I would like to put elements on top
Sorry if the title doesn't make sense. Basically I have a series of strings
Basically, what the title says. I have several properties that combine together to really
See title. Basically, the data in this report is set up such that each
Basically, what the question title says. Thread t = new Thread(someRunnable); t.start(); t.interrupt(); t.join();
Amazingly, the title basically explains it. We have a page that our client made
Sorry for the confusing title. Basically I made a BinaryReader class that reads in
The title basically says it all. I mainly want to do this so that
The title description basically says it all. I'd like to use something that requires

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.