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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:57:45+00:00 2026-06-13T11:57:45+00:00

I have a doubt regarding pthread_cond_wait and pthread_cond_signal functions. I was not able to

  • 0

I have a doubt regarding pthread_cond_wait and pthread_cond_signal functions. I was not able to understand after reading the man pages also.

Please consider the following code.

void* thread_handler(){
... // counts till COUNT_LIMIT is reached
if (count == COUNT_LIMIT) {
  pthread_cond_signal(&count_threshold_cv);
  printf("inc_count(): thread %ld, count = %d  Threshold reached.\n",
         my_id, count);
}
printf("inc_count(): thread %ld, count = %d, unlocking mutex\n",
       my_id, count);
...
}

void* thread_handler1(){
... // waits till the previous thread has finished counting
pthread_mutex_lock(&count_mutex);
while (count<COUNT_LIMIT) {
   pthread_cond_wait(&count_threshold_cv, &count_mutex);
   printf("watch_count(): thread %ld Condition signal received.\n", my_id);
}
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}

The code is working as per expectations. I am trying to understand the code. Here is how the program works

  1. Enter thread_handler1 and do cond_wait. From the man page i understood that cond_wait will immediatly release the lock atomically. So why are they releasing the lock again below in thread_handler1

  2. After first thread has satisfied the condition and hits the condition signal I expected the thread which was blocking to execute its steps. Instead i got the printfs below the thread which executed the cond_signal. Why is this happening

  3. Overall why do we need to take a lock before waiting and signalling. Cant this be done without a lock.

For a briefer look at the program, please see the Complete program here. You can find it under the section “Using Conditional Variables”

Thanks in advance

Chidambaram

  • 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-13T11:57:46+00:00Added an answer on June 13, 2026 at 11:57 am

    Enter thread_handler1 and do cond_wait. From the man page i understood
    that cond_wait will immediatly release the lock atomically. So why are
    they releasing the lock again below in thread_handler1

    Because the thread that is supposed to wait will release the lock when calling wait, but will then reacquire after it gets signaled (when it becomes available). That’s why you need to rerelease it explicitly later.

    After first thread has satisfied the condition and hits the condition
    signal I expected the thread which was blocking to execute its steps.
    Instead i got the printfs below the thread which executed the
    cond_signal. Why is this happening

    Because calling signal does not switch the thread from the CPU. It will continue to run normally.

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

Sidebar

Related Questions

I am reading linux device driver book of rubini,corbet and hartmen.I have doubt regarding
I have a doubt regarding the backlog value in listen system call. From man
I have little doubt about string reading in C. string reading functions like gets,
I have one doubt regarding xml reading using Stax Parser. Expected Result: hq_seq_mast {SEQ_VAL=SEQ_VAL,
I have a doubt regarding functions(objects) in javascript. I have 2 pieces of code
i have small doubt regarding the window functions in c++. what exactly invalidate() function
I have a doubt regarding the concept of encapsulation. Isn't C language also does
I have a doubt regarding Java Synchronization . I want to know if I
I have one doubt regarding casting. public void Test(out T a, out T b)
I have the doubt regarding when to use the Dispatcher.Invoke to update something 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.