Transferred fromHttp://weihe6666.iteye.com/blog/1170141  
 
A condition variable is a mechanism for synchronizing global variables shared by threads. It mainly includes two actions: one thread waits for the condition variable to be established and suspends; another thread sets "condition to true" (a signal indicating condition to true ). To prevent competition, the use of condition variables is always combined with a mutex lock.
 
 
 
A pthread_cond_wait definition:
 
 
 
Function prototype: int pthread_cond_wait (pthread_cond_t * cond, pthread_mutex_t * mutex)
 
 
 
Parameter: cond condition variable mutex lock
The first parameter * cond is a pointer to a condition variable. The second parameter * mutex is the pointer to the related mutex lock.
 
 
 
Pthread_cond_wait example
 
 
 
Pthread_cond_wait is an important component of conditional variables. If the condition variable is used for synchronization between threads, pthread_cond_wait must be used together with the mutex lock in a thread. It also locks and unlocks resources. See the following example:
 
 
 
ProgramTwo new threads have been created for them to run synchronously. The T_ B process prints a multiple of 3 within 9, and T_A prints other numbers. At the beginning of the program, thread T_ B does not meet the conditions to wait, thread T_A runs to add 1 to a loop and print it. When I is a multiple of 3, thread T_A sends a signal to the process T_ B, and then T_ B meets the condition and prints the I value.
 
 
 1 # Include <pthread. h> 2 # Include <unistd. h> 3 # Include <stdio. h> 4 # Include <stdlib. h> 5   6 Pthread_mutex_t mutex = pthread_mutex_initializer; /*  Initialize mutex lock  */  7 Pthread_cond_t cond = pthread_cond_initializer; //  Init cond  8   9  Void * Thread1 ( Void * );  10   Void * Thread2 ( Void * );  11   12   Int I = 1 ; //  Global  13   14   Int Main ( Void  ){  15   Pthread_t T_A;  16 Pthread_t T_ B; //  Two thread  17   18 Pthread_create (& T_A, null, thread2 ,( Void * ) Null );  19 Pthread_create (& T_ B, null, thread1 ,( Void *) Null ); // Create thread  20   21 Pthread_join (T_ B, null ); //  Wait a_ B thread end  22 Pthread_mutex_destroy (& Mutex );  23 Pthread_cond_destroy (& Cond );  24 Exit ( 0  );  25   }  26  27   Void * Thread1 ( Void * Junk ){  28       For (I = 1 ; I <= 9 ; I ++ ){  29 Pthread_mutex_lock (& mutex ); //  Mutex lock  30 Printf ( " Call thread1 \ n  "  );  31           If (I % 3 = 0  )  32 Pthread_cond_signal (& Cond ); //  Send sianal to T_ B  33           Else  34 Printf ( " Thread1: % d \ n  "  , I );  35 Pthread_mutex_unlock (& Mutex );  36 Sleep ( 1  );  37   }  38   }  39   40   Void * Thread2 ( Void *Junk ){  41       While (I < 9  )  42   {  43 Pthread_mutex_lock (& Mutex );  44 Printf ( "  Call thread2 \ n  "  );  45           If (I %3 ! = 0  )  46 Pthread_cond_wait (& cond, & mutex ); //  Wait  47 Printf ( "  Thread2: % d \ n  "  , I );  48 Pthread_mutex_unlock (& Mutex );  49 Sleep (1  );  50   }  51 } 
 
View code
 
 
 
Output:
Call thread2
Call thread1
Thread1: 1
Call thread1
Thread1: 2
Call thread1
Thread2: 3
Call thread1
Thread1: 4
Call thread2
Call thread1
Thread1: 5
Call thread1
Thread2: 6
Call thread1
Thread1: 7
Call thread2
Call thread1
Thread1: 8
Call thread1
Thread2: 9
 
 
Example:
Call thread2: It is thread 2, that is, T_ B is first locked, that is
Pthread_mutex_lock (& mutex); the mutex lock makes the critical section in thread 2 of this ProcessCodeWhen 45 rows are executed: If (I % 3
! = 0). At this time, I = 1. If this condition is met, 46 rows are executed: pthread_cond_wait (& cond, & mutex );
This sentence is the key,The pthread_cond_wait (& cond, & mutex) operation has two steps, which are atomic operations: the first
Unlock: First remove the mutex locked by pthread_mutex_lock; second
Pending, blocking, and waiting for sleep in the column, That is, thread 2 is suspended until it is awakened again. The wake-up condition is sent by pthread_cond_signal (& Cond );
Cond signal to wake up.
 
 
 
Call thread1: Because pthread_cond_wait has already unlocked thread 2 and another thread only has thread 1, thread 1 locks mutex. If there are multiple threads, the lock sequence between threads is related to the operating system.
 
 
 
Thread1: 1: After thread 1 is locked, the code of the critical section is executed. When it is executed to if (I % 3 =
0) at this time I = 1, does not meet the conditions, then pthread_cond_signal (& Cond); is not executed, then thread 2 is still suspended, output
Thread1: thread 1 is unlocked by pthread_mutex_unlock (& mutex.
 
 
 
Thread1:
2: At this time, there are only two threads in the process, thread 2 is suspended, so only thread 1 is available, and thread 1 is locked to mutex. At this time, the code in the critical section is also executed, and I = 2, not satisfied
, Pthread_cond_signal (& Cond); is not executed, so thread 2 is still suspended and thread1 is output:
Thread 1 is unlocked by pthread_mutex_unlock (& mutex.
 
 
Call
Thread1: it is also locked by thread 1, but at this time I = 3, the condition pthread_cond_signal (& Cond) is executed, then
Pthread_cond_signal (& Cond) sends a signal to wake up the suspended thread 2.Pthread_cond_signal is composed of two
Atomic operation: 1, unlock; 2, send signalUnlock: Unlock thread 1 and unlock mutex. Send a signal, that is, send a signal to thread 2 waiting for signal to be suspended, and wake up the suspended thread.
2.
 
 
 
Thread2:
3: Because pthread_cond_signal wakes up thread 2, that is, I = 3 meets the condition, pthread_cond_wait (& cond ,&
Mutex); executed,Pthread_cond_wait (& cond, & mutex) also has one operation: Lock; that is, lock thread 2,
In this case, the pthread_cond_wait (& cond, & mutex) operations are equivalent
Pthread_mutex_lock (& mutex); Then thread 2 continues to execute the code of the locked critical section, and
Pthread_mutex_unlock (& mutex); unlock thread 2.
 
 
 
The remaining output principles are the same as described above.
 
 
 
Looking at pthread_cond_wait, it cannot be understood as a simple wait function. It should be a family of functions, and different functions are executed under different conditions, understanding the pthread_cond_wait mechanism can be a good learning condition variable.