c - Wait on multiple condition variables on Linux without unnecessary sleeps? -


I am writing a latency sensitive app which wants to wait at several condition variables at once. I have read several ways to get this functionality on Linux (apparently it's built on Windows), but none of them is suitable for my app. The methods I know about are:

  1. Wait for a wait at each status variable, which you would like to wait for, when a single variable indicates,

  2. Cycling through several condition variables with a timely wait.

  3. Instead of writing dummy bytes for files or pipes, and voting on them

# 1 & amp; # 2 is inappropriate because they cause unnecessary sleep. With # 1, you will have to wait for the dummy thread to wake up, then point to the real thread, then to wake the real thread, instead of waking up to start instead of the actual thread - the extra scheduler spent on the quantum I mean, for my app, and I would not want to use a full RTOS # 2 is worse, you likely sleep in time *, or you're out of time. Received, in which case you can never sleep (eventually die CPU burn and other threads hunger is worse). For p # 3, pipe is problematic because if the thread 'signal' is busy or even crashing (I'm actually working with different process instead of threads - Mutex And the terms will be stored in shared memory), then the writing thread will be stuck because the buffer buffer will be completed, as any other customer will have the files are problematic because you want to increase it and the app runs now Or being.

Is there a better way to do this? Also eager to answer for Solaris appropriate answer.

If you are talking about posix thread, then the number of single condition variable and event flag Or use something similar. The idea is to use peer condor mutes to preserve event notifications. You must check the event after exit (cond_wait) anyway. My training is my old enough code to make it clear (yes, I checked that it runs, but please note that, a while ago and quickly prepared for newcomers).

  #include & lt; Pthread.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Unistd.h & gt; Static pthread_cond_t var; Fixed pthread_mutex_t mtx; Unsigned event_flags = 0; # Define FLAG_EVENT_1 1 # Define FLAG_EVENT_2 2 Zero Signals_1 () {pthread_mutex_lock (& ​​amp; mtx); Event_flags | = FLAG_EVENT_1; Pthread_cond_signal (& amp; var); Pthread_mutex_unlock (& ​​amp; MTX); } Zero signal_2 () {pthread_mutex_lock (& ​​amp; mtx); Event_flags | = FLAG_EVENT_2; Pthread_cond_signal (& amp; var); Pthread_mutex_unlock (& ​​amp; MTX); } Zero * handler (Zero *) {// Mutex is unlocked when we wait or process the event received pthread_mutex_lock (& ​​amp; MTX); // The real code should contain race-state prevention. While (1) {if (event_flags) {unsigned copy = event_flags; // We unlock the Mute X until we are processing events that are receiving pthread_mutex_unlock (& ​​amp; MTX); If (event_flags & amp; FLAG_EVENT_1) {printf ("EVENT 1 \ n"); Event_flags ^ = FLAG_EVENT_1; } If (event_flags & amp; FLAG_EVENT_2) {printf ("EVENT 2 \ n"); Event_flags ^ = FLAG_EVENT_2; // and the signal to stop incident 2. // In this case for the stability we break off with Mute X. Pthread_mutex_lock (& ​​amp; MTX); break; } // Note that we should lock the mutex at the end of the recurrence. Pthread_mutex_lock (& ​​amp; MTX); } Else {// Mutex is locked when we are waiting; it is open Pthread_cond_wait (& amp; var; & amp; MTX;); // mutics is locked}} // ... because we're dying Pthread_mutex_unlock (& ​​amp; MTX); } Int main () {pthread_mutex_init (& amp; MTX, NULL); Pthread_cond_init (& amp; var, NULL); Pthread_t ID; Pthread_create (& amp; id, NULL, handler, NULL); Sleep (1); Signal_1 (); Sleep (1); Signal_1 (); Sleep (1); Signal_2 (); Sleep (1); Pthread_join (id, NULL); Return 0; }  

Comments