等待線程
1。使用pthread_cond_wait前要先加鎖 2。pthread_cond_wait內(nèi)部會(huì)解鎖,然后等待條件變量被其它線程激活 3。pthread_cond_wait被激活后會(huì)再自動(dòng)加鎖 激活線程: 1。加鎖(和等待線程用同一個(gè)鎖) 2。pthread_cond_signal發(fā)送信號(hào) 3。解鎖 激活線程的上面三個(gè)操作在運(yùn)行時(shí)間上都在等待線程的pthread_cond_wait函數(shù)內(nèi)部。 程序示例: #include <stdio.h> #include <pthread.h> #include <unistd.h> pthread_mutex_t count_lock; pthread_cond_t count_nonzero; unsigned count = 0; void * decrement_count(void *arg) { pthread_mutex_lock (&count_lock); printf("decrement_count get count_lock\n"); while(count==0) { printf("decrement_count count == 0 \n"); printf("decrement_count before cond_wait \n"); pthread_cond_wait( &count_nonzero, &count_lock); printf("decrement_count after cond_wait \n"); } count = count -1; pthread_mutex_unlock (&count_lock); } void * increment_count(void *arg){ pthread_mutex_lock(&count_lock); printf("increment_count get count_lock\n"); if(count==0) { printf("increment_count before cond_signal\n"); pthread_cond_signal(&count_nonzero); printf("increment_count after cond_signal\n"); } count=count+1; pthread_mutex_unlock(&count_lock); } int main(void) { pthread_t tid1,tid2; pthread_mutex_init(&count_lock,NULL); pthread_cond_init(&count_nonzero,NULL); pthread_create(&tid1,NULL,decrement_count,NULL); sleep(2); pthread_create(&tid2,NULL,increment_count,NULL); sleep(10); pthread_exit(0); } 2.decrement_count和increment_count在兩個(gè)線程A和B中被調(diào)用。
正確的情況下,如果decrement_count首先運(yùn)行,那么A會(huì)被阻塞到pthread_cond_wait。隨后increment_count運(yùn)行,它調(diào)用pthread_cond_signal喚醒等待條件鎖count_nonzero的A線程,但是A線程并不會(huì)馬上執(zhí)行,因?yàn)樗貌坏交コ怄icount_lock。當(dāng)B線程執(zhí)行pthread_mutex_unlock之后A線程才得以繼續(xù)執(zhí)行。
如果pthread_cond_signal前后沒有使用互斥鎖count_lock保護(hù),可能的情況是這樣。A阻塞到pthread_cond_wait,然后B執(zhí)行到pthread_cond_signal時(shí)候,發(fā)生了線程切換,于是A被喚醒,并且發(fā)現(xiàn)count依然是0,所以繼續(xù)阻塞到條件鎖count_nonzero上。然后B繼續(xù)執(zhí)行,這時(shí)候盡管count=1,A永遠(yuǎn)不會(huì)被喚醒了。這樣就發(fā)生了邏輯錯(cuò)誤。
當(dāng)然在這個(gè)上下文中,如果把count=count+1放在函數(shù)放在pthread_cond_signal之前變成
increment_count(){
} 這樣沒有問題。但是這種方法并不能保證所有情況下都適用。于是需要用互斥鎖保護(hù)條件鎖相關(guān)的變量。也就是說條件鎖是用來線程通訊的,但是互斥鎖是為了保護(hù)這種通訊不會(huì)產(chǎn)生邏輯錯(cuò)誤,可以正常工作。
|
|