pthread_cond_wait

163 阅读2分钟

Condition Variable Operation Semantics

  • pthread_cond_wait (&CV, &M)

    • should only be called by a thread that holds locked mutex M
    • unlocks the mutex M
    • gives other threads a chance to execute
    • may suspend the calling thread until CV is signalled
    • relocks the mutex (blocking until the mutex becomes available) before returning
    • must always be used in a loop that polls for a meaningful logical condition involving other variables
    • may not check for most errors
    • effects of errors are mostly undefined
  • pthread_cond_signal (&CV)

    • does not require that any mutex lock be held

    • ensures one or more of the threads waiting on CV (if any) wake up
      (and begin trying to acquire the associated mutex)

    • does not check for most errors, including the error of unlocking a mutex whose lock is not held by the calling thread

    • effects of errors are mostly undefined

The main thing about pthread_cond_wait is that it must release the mutex, give other threads a chance to lock (and then unlock) the mutex, and then relock the mutex. (Whether the thread goes to sleep during the interval in which the mutex is unlocked is up to the implementation.)

The main thing about pthread_cond_signal is that if any threads are sleeping on the CV, at least one of them will wake up, lock the mutex, and recheck the logical condition. (Whether it wakes up one thread or all the threads that are waiting on the CV is up to the implementation.)

pthread_cond_wait() functions shall block on a condition variable. They shall be called with mutex locked by the calling thread or undefined behavior results.

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to pthread_cond_broadcast() or pthread_cond_signal() in that thread shall behave as if it were issued after the about-to-block thread has blocked.

Upon successful return, the mutex shall have been locked and shall be owned by the calling thread.

A Simple Example

A simple example of using condition variables is in the program barrier.c. Here, we have 5 threads, and we want to make sure that they all synchronize at a particular point. Often this is called a ``barrier'', since all the threads stop at this barrier before proceeding. In barrier.c the number of threads waiting is held in the variable ndone, and if a thread reaches the barrier before ndone equals NTHREADS, it waits on the condition variable ts->cv. When the last thread reaches the barrier, it wakes all the others up using pthread_cond_signal. The output of barrier.c shows that they all block until the last thread reaches the barrier:

UNIX> barrier
Thread 0 -- waiting for barrier
Thread 1 -- waiting for barrier
Thread 2 -- waiting for barrier
Thread 3 -- waiting for barrier
Thread 4 -- waiting for barrier
Thread 4 -- after barrier
Thread 0 -- after barrier
Thread 1 -- after barrier
Thread 2 -- after barrier
Thread 3 -- after barrier
done
UNIX>

参考:

web.eecs.utk.edu/~huangj/cs3… www.cs.fsu.edu/~baker/opsy… linux.die.net/man/3/pthre…