A simple question about pthread_cond_wait()

This is a discussion on A simple question about pthread_cond_wait() within the C++ Programming forums, part of the General Programming Boards category; As we know, pthread_cond_wait(cond, mutex) first unlock the mutex and wait. When it is waked up on condition variable, it ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    A simple question about pthread_cond_wait()

    As we know, pthread_cond_wait(cond, mutex) first unlock the mutex and wait. When it is waked up on condition variable, it has to lock the mutex.
    Now my question is: what if it's waked up but found the mutex is already locked by someone else? What shall it do? Will it go back to sleep(wait) or just give up?

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah... I am trying to remember the correct euphamism for this one... The spaghetti eating philosopher's paradox, methinks.

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    >> What if it's waked up but found the mutex is already locked by someone else?
    Not a relevant question for a user of the function. All the user needs to know is that: "Upon successful return, the mutex shall have been locked and shall be owned by the calling thread."

    gg

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The implementation will wait until it can lock the mutex. Then it will check the condition. If it's false, it will unlock the mutex and go back to sleep.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,273
    Quote Originally Posted by CornedBee View Post
    The implementation will wait until it can lock the mutex. Then it will check the condition. If it's false, it will unlock the mutex and go back to sleep.
    That isn't true. The mutex might be re-acquired even though the condition no longer holds. This is why all calls to pthread_cond_wait() should be wrapped in a while (not an if!) which tests the condition. Some other thread could intervene and cause the condition to become untrue even though the testing thread has woken up.

    In general:

    Code:
    pthread_mutex_lock(&mut);
    while(!some_condition)
    {
        pthread_cond_wait(&cond, &mut);
    }
    /* Do something to the state... */
    pthread_mutex_unlock(&mut);
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Sorry, I forgot that most condition variables don't actually have the condition integrated.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,701
    Quote Originally Posted by CornedBee View Post
    The implementation will wait until it can lock the mutex. Then it will check the condition. If it's false, it will unlock the mutex and go back to sleep.
    In terms of what's going on inside pthread_cond_wait(), that's basically what it's doing. Where "check the condition" == "check if the pthread_cond_t is signaled".

    In terms of the external predicate associated with the pthread_cond_t, brewbuck is also right due to "spurious wakeups". This is where multiple threads waiting on the same condition can successfully return from pthread_cond_wait() after a single call to pthread_cond_signal().

    More reading on "spurious wakeups" in the "Rationale" section here: http://www.opengroup.org/onlinepubs/...nd_signal.html

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21