Thread: A quick question about pthread_cond_wait()

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

    A quick question about pthread_cond_wait()

    Suppose multiple producers and consumers share a common queue.
    Consumers are signaled when any producer puts data to the queue.

    1. Does the following code has race condition?

    Code:
    //Producer:
    pthread_mutex_lock(&lock);
    //put data to a queue
    pthread_cond_broadcast(&cond);
    pthread_mutex_unlock(&lock);

    Code:
    //Consumer:
    pthread_cond_wait(&cond, &lock);
    //get data from the queue
    pthread_mutex_unlock(&lock);
    2. If I move the line pthread_cond_broadcast(&cond); out of mutex box as:
    Code:
    //Producer:
    pthread_mutex_lock(&lock);
    //put data to a queue
    pthread_mutex_unlock(&lock);
    pthread_cond_broadcast(&cond);
    Does it has the same effect as the above code snippet?
    Last edited by meili100; 09-27-2008 at 11:40 PM.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe it does the same thing. The second seems better, since dunno for sure what will happen if you send a signal to a thread which is locked from some thread calling mutex_lock

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Both are safe, but are you sure you want to wake all threads? Most likely the producer only added one item, right?

    Edit: But I think the first one is better. It avoids spurious wakeups of consumers in one very rare situation.
    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

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    With condition variable semantics, you need to have a predicate due to "spurious wakeups". If you have a predicate then calling signal/broadcast with the mutex locked or unlocked will both result in defined behavior.

    You can read more on spurious wakeups at the bottom of the signal/broadcast documentation: http://www.opengroup.org/onlinepubs/...broadcast.html

    You can read more on the implications of calling signal/broadcast with the mutex locked/unlocked here: http://groups.google.com/group/comp....e7ffb2f5264496

    gg
    Last edited by Codeplug; 09-28-2008 at 02:32 PM. Reason: fixed links

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    284
    Thanks all,
    gg: Your links don't work?
    CornedBee: I still don't understand why the first is better in terms of avoiding spurious wakeups. Would you elaborate more?
    Last edited by meili100; 09-28-2008 at 12:33 PM.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Hmm ... must have been a brain fart.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Very quick math question
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 10-26-2005, 11:05 PM
  2. very quick question.
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 07-24-2002, 03:48 AM
  3. quick question
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2002, 04:44 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM