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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:19:14+00:00 2026-06-05T19:19:14+00:00

From here: https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal Note that the pthread_cond_wait routine will automatically and atomically unlock mutex

  • 0

From here: https://computing.llnl.gov/tutorials/pthreads/#ConVarSignal

Note that the pthread_cond_wait
routine will automatically and atomically unlock mutex while it waits.

The following sub-code is from the same link (formatting by me):

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);
      count += 125;
      printf("watch_count(): thread %ld count now = %d.\n", my_id, count);
  }

pthread_mutex_unlock(&count_mutex);  

Question:
When it says that pthread_cond_wait will automatically unlock mutex while it waits, then why do we have to explicitly specify the function pthread_mutex_unlock at the end of the code above?

What’s the point that I am missing?

  • 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-05T19:19:16+00:00Added an answer on June 5, 2026 at 7:19 pm

    When pthread_cond_wait unblocks it is holding the lock again. Say for example you go around the loop twice you get the following sequence of lock/unlocks on the mutex:

    lock
    
    # Around loop twice:
        wait (unlock)
        awaken (holding lock)
        wait (unlock)
        awaken (holding lock)
    
    # loop done, still holding lock
    
    unlock
    

    If you don’t have that last unlock there then you’ll end up with deadlock the next time someone else wants to acquire the lock.

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

Sidebar

Related Questions

From here: https://stackoverflow.com/a/2485177/462608 For thread-safe accesses to shared data, we need a guarantee that
From here: https://stackoverflow.com/a/5524120/462608 If you want to lock several mutex-protected objects from a set
I try to install rails facebooker to my authlogic gems from here https://github.com/kalasjocke/authlogic_facebook_connect ,
I'm having troubles using the facebook binding from here ( https://github.com/mono/monotouch-bindings/tree/master/facebook ) and the
From here: http://google-glog.googlecode.com/svn/trunk/doc/glog.html Debug Mode Support Special debug mode logging macros only have an
I used this example from here http://www.asp.net/ajaxlibrary/jqueryui_progressbar.ashx Every thing works fine except the progress
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
I'm trying with example from here http://msdn.microsoft.com/en-us/library/ff407126 I would like to add a control
I downloaded the swc file from here: http://developer.yahoo.com/maps/flash/flexGettingStarted.html And put it in my flex
I'm using the jquery mobile datebox from here: http://dev.jtsage.com/jQM-DateBox/ and I am setting the

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.