带条件变量的多线程.
A condition variable is a variable of type pthread_cond_t and is used with the appropriate functions for waiting and later, process continuation. The condition variable mechanism allows threads to suspend execution and relinquish the processor until some condition is true. A condition variable must always be associated with a mutex to avoid a race condition created by one thread preparing to wait and another thread which may signal the condition before the first thread actually waits on it resulting in a deadlock. The thread will be perpetually waiting for a signal that is never sent. Any mutex can be used, there is no explicit link between the mutex and the condition variable.
Functions used in conjunction with the condition variable:
#include <stdio.h> #include <stdlib.h> #include <pthread.h> pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t condition_mutex = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t condition_cond = PTHREAD_COND_INITIALIZER; void *functionCount1(); void *functionCount2(); int count = 0; #define COUNT_DONE 10 #define COUNT_HALT1 3 #define COUNT_HALT2 6 main() { pthread_t thread1, thread2; pthread_create( &thread1, NULL, &functionCount1, NULL); pthread_create( &thread2, NULL, &functionCount2, NULL); pthread_join( thread1, NULL); pthread_join( thread2, NULL); exit(0); } void *functionCount1() { for(;;) { pthread_mutex_lock( &condition_mutex ); while( count >= COUNT_HALT1 && count <= COUNT_HALT2 ) { pthread_cond_wait( &condition_cond, &condition_mutex ); } pthread_mutex_unlock( &condition_mutex ); pthread_mutex_lock( &count_mutex ); count++; printf("Counter value functionCount1: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(NULL); } } void *functionCount2() { for(;;) { pthread_mutex_lock( &condition_mutex ); if( count < COUNT_HALT1 || count > COUNT_HALT2 ) { pthread_cond_signal( &condition_cond ); } pthread_mutex_unlock( &condition_mutex ); pthread_mutex_lock( &count_mutex ); count++; printf("Counter value functionCount2: %d\n",count); pthread_mutex_unlock( &count_mutex ); if(count >= COUNT_DONE) return(NULL); } }
Results:
Counter value functionCount1: 1 Counter value functionCount1: 2 Counter value functionCount1: 3 Counter value functionCount2: 4 Counter value functionCount2: 5 Counter value functionCount2: 6 Counter value functionCount2: 7 Counter value functionCount1: 8 Counter value functionCount1: 9 Counter value functionCount1: 10 Counter value functionCount2: 11
Note that functionCount1() was halted while count was between the values COUNT_HALT1 and COUNT_HALT2. The only thing that has been ensures is that functionCount2 will increment the count between the values COUNT_HALT1 and COUNT_HALT2. Everything else is random.
The logic conditions (the "if" and "while" statements) must be chosen to insure that the "signal" is executed if the "wait" is ever processed. Poor software logic can also lead to a deadlock condition.
Note: Race conditions abound with this example because count is used as the condition and can't be locked in the while statement without causing deadlock. I'll work on a cleaner example but it is an example of a condition variable.