pthread_cond

條件變量 pthread_cond, 另外一種線程間的同步機制。普通的 mutex 只允許一個線程進入臨界區,就是拿到mutex這把鎖的線程,而cond 允許多個線程同時進入臨界區,由它來控制,在某些條件成立的時候,來喚醒其中一個等待着的線程,或者是喚醒所有等待着的線程。

int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex);
int pthread_cond_timewait(pthread_cond_t* cond, pthread_mutex_t* mutex, const struct timespec* tout)

傳遞給pthrad_cond_wait 的互斥量 mutex 對條件進行保護,調用者把鎖住的互斥量傳遞給pthread_cond_wait 函數,函數把調用線程放到等待條件的線程列表裏面,然後對互斥量解鎖,當pthread_cond_wait 返回的時候,互斥量再次被鎖住。函數 pthread_cond_timewait 與 pthread_cond_wait 差不多,只不過是多了一個超時時間的限制。
兩個函數調用成功返回的時候,需要重新檢查條件,因爲其他線程可能更改了條件。

int pthread_cond_signal(pthread_cond_t* cond);
int pthread_cond_broadcast(pthread_cond_t* cond);

pthread_cond_signal 函數將喚醒等待該條件的某個線程,pthread_cond_broadcast 將喚醒等待改條件的所有線程。

下面的例子很簡單的使用了 cond 。 使用cond 我們可以比較高效的寫出一個 線程池。

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
pthread_mutex_t mutex;
pthread_cond_t cond;
int val = 0;
void *thread_zero_run(void *arg){
	while(1){
		pthread_mutex_lock(&mutex); // lock the mutex
		while(val <= 2){ //condition.  use while for double check
			printf("thread_zero_run --> val:%d, wait for wake up\n", val);
			pthread_cond_wait(&cond, &mutex);//call pthread_cond_wait
		}
	printf("therad_zero_run --> val:%d, zero it and unlock\n", val);
	val = 0; //do  sth
	pthread_mutex_unlock(&mutex); //unlock the mutex
	}
   pthread_exit((void*)0);
}
void *thread_add_run(void *arg){
	while(1){
	pthread_mutex_lock(&mutex); //lock the mutex
 	++val; //do sth
 	pthread_mutex_unlock(&mutex); //unlock the mutex
 	pthread_cond_signal(&cond); //wake up a therad whick is waiting for the cond
	printf("after add val:%d and wake up one zero thread for check\n", val);
	sleep(1);
	}
   pthread_exit((void*)0);
}
int main(){
	pthread_t t_add, t_zero;
   pthread_cond_init(&cond, NULL);
   if(pthread_create(&t_add, NULL, thread_add_run, NULL)){
     return 0;
   }
   if(pthread_create(&t_zero, NULL, thread_zero_run, NULL)){
     return 0;
   }
   pthread_join(t_add, NULL);
   pthread_join(t_zero, NULL);
   return 0;
}

輸出:

after add val:1 and wake up one zero thread for check
thread_zero_run --> val:1, wait for wake up
after add val:2 and wake up one zero thread for check
thread_zero_run --> val:2, wait for wake up
after add val:3 and wake up one zero thread for check
therad_zero_run --> val:3, zero it and unlock
thread_zero_run --> val:0, wait for wake up
after add val:1 and wake up one zero thread for check
thread_zero_run --> val:1, wait for wake up
after add val:2 and wake up one zero thread for check
thread_zero_run --> val:2, wait for wake up
after add val:3 and wake up one zero thread for check
therad_zero_run --> val:3, zero it and unlock
thread_zero_run --> val:0, wait for wake up
after add val:1 and wake up one zero thread for check
thread_zero_run --> val:1, wait for wake up
after add val:2 and wake up one zero thread for check
thread_zero_run --> val:2, wait for wake up
after add val:3 and wake up one zero thread for check
therad_zero_run --> val:3, zero it and unlock
thread_zero_run --> val:0, wait for wake up

轉自:http://www.cppblog.com/MemoryGarden/archive/2011/06/08/148241.html

發佈了23 篇原創文章 · 獲贊 35 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章