A common requirement for many Linux applications is that an event on one thread should trigger some processing on another. For example, in a video conferencing application, when a video frame is received through RTP, it should trigger the decoding process. Decoding may take a long time, so it should be done in a separate thread so it doesn’t block further RTP processing. There are many ways to handle these situations. Two possibilities are polling and pthread conditions. With polling, the processing thread is frequently waking up and checking for a condition. When the condition is met, it does the processing. With pthread conditions, the Linux kernel handles waking the processing thread when another thread signals the condition. This can be much more efficient than polling, especially for infrequent events.
Using pthread conditions requires two objects: a mutex and a condition. First, both objects are created and initialized (pthread_mutex_init, pthread_cond_init). After this, the processing thread should lock the mutex (pthread_mutex_lock), and then wait for the condition (pthread_cond_wait). The wait function will un-lock the mutex while the thread is suspended. Any thread that wants to signal the processing thread needs both the mutex and condition objects. First it should lock the mutex. Then it can signal the processing thread (pthread_cond_signal) and then unlock the mutex (pthread_mutex_unlock). This sequence will wake up the processing thread. When the processing thread returns from the wait function, it will have the mutex locked again. It may choose to unlock the mutex, or keep it locked during processing depending on the application requirements.