多线程编程-条件变量
轉(zhuǎn)載自:http://blog.chinaunix.net/uid-21411227-id-1826890.html
1.引言:
條件變量是一種同步機(jī)制,允許線程掛起,直到共享數(shù)據(jù)上的某些條件得到滿足。條件變量上的基本操作有:觸發(fā)條件(當(dāng)條件變?yōu)?span style="font-family:'Times New Roman';">?true?時(shí));等待條件,掛起線程直到其他線程觸發(fā)條件。
????條件變量要和互斥量相聯(lián)結(jié),以避免出現(xiàn)條件競(jìng)爭(zhēng)--一個(gè)線程預(yù)備等待一個(gè)條件變量,當(dāng)它在真正進(jìn)入等待之前,另一個(gè)線程恰好觸發(fā)了該條件。
2.函數(shù)說明:
1)初始化條件變量pthread_cond_init
函數(shù)原型:int pthread_cond_init(pthread_cond_t *cv, const pthread_condattr_t *cattr);
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤。
參數(shù)說明:當(dāng)參數(shù)cattr為空指針時(shí),函數(shù)創(chuàng)建的是一個(gè)缺省的條件變量。否則條件變量的屬性將由cattr中的屬性值來決定。調(diào)用pthread_cond_init函數(shù)時(shí),參數(shù)cattr為空指針等價(jià)于cattr中的屬性為缺省屬性,只是前者不需要cattr所占用的內(nèi)存開銷。這個(gè)函數(shù)返回時(shí),條件變量被存放在參數(shù)cv指向的內(nèi)存中。
可以用宏PTHREAD_COND_INITIALIZER來初始化靜態(tài)定義的條件變量,使其具有缺省屬性。這和用pthread_cond_init函數(shù)動(dòng)態(tài)分配的效果是一樣的。初始化時(shí)不進(jìn)行錯(cuò)誤檢查。如:pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
不能由多個(gè)線程同時(shí)初始化一個(gè)條件變量。當(dāng)需要重新初始化或釋放一個(gè)條件變量時(shí),應(yīng)用程序必須保證這個(gè)條件變量未被使用。
2)阻塞在條件變量上pthread_cond_wait
函數(shù)原型:int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex);
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤
參數(shù)說明:函數(shù)將解鎖mutex參數(shù)指向的互斥鎖,并使當(dāng)前線程阻塞在cv參數(shù)指向的條件變量上。被阻塞的線程可以被pthread_cond_signal函數(shù),pthread_cond_broadcast函數(shù)喚醒,也可能在被信號(hào)中斷后被喚醒。pthread_cond_wait函數(shù)的返回并不意味著條件的值一定發(fā)生了變化,必須重新檢查條件的值。pthread_cond_wait函數(shù)返回時(shí),相應(yīng)的互斥鎖將被當(dāng)前線程鎖定,即使是函數(shù)出錯(cuò)返回。
一般一個(gè)條件表達(dá)式都是在一個(gè)互斥鎖的保護(hù)下被檢查。當(dāng)條件表達(dá)式未被滿足時(shí),線程將仍然阻塞在這個(gè)條件變量上。當(dāng)另一個(gè)線程改變了條件的值并向條件變量發(fā)出信號(hào)時(shí),等待在這個(gè)條件變量上的一個(gè)線程或所有線程被喚醒,接著都試圖再次占有相應(yīng)的互斥鎖。阻塞在條件變量上的線程被喚醒以后,直到pthread_cond_wait()函數(shù)返回之前條件的值都有可能發(fā)生變化。所以函數(shù)返回以后,在鎖定相應(yīng)的互斥鎖之前,必須重新測(cè)試條件值。最好的測(cè)試方法是循環(huán)調(diào)用pthread_cond_wait函數(shù),并把滿足條件的表達(dá)式置為循環(huán)的終止條件。
如:pthread_mutex_lock(); while (condition_is_false) pthread_cond_wait(); pthread_mutex_unlock();
阻塞在同一個(gè)條件變量上的不同線程被釋放的次序是不一定的。
注意:pthread_cond_wait()函數(shù)是退出點(diǎn),如果在調(diào)用這個(gè)函數(shù)時(shí),已有一個(gè)掛起的退出請(qǐng)求,且線程允許退出,這個(gè)線程將被終止并開始執(zhí)行善后處理函數(shù),而這時(shí)和條件變量相關(guān)的互斥鎖仍將處在鎖定狀態(tài)。
3)解除在條件變量上的阻塞pthread_cond_signal
函數(shù)原型:int pthread_cond_signal(pthread_cond_t *cv);
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤
參數(shù)說明:函數(shù)被用來釋放被阻塞在指定條件變量上的一個(gè)線程。必須在互斥鎖的保護(hù)下使用相應(yīng)的條件變量。否則對(duì)條件變量的解鎖有可能發(fā)生在鎖定條件變量之前,從而造成死鎖。
喚醒阻塞在條件變量上的所有線程的順序由調(diào)度策略決定,如果線程的調(diào)度策略是SCHED_OTHER類型的,系統(tǒng)將根據(jù)線程的優(yōu)先級(jí)喚醒線程。如果沒有線程被阻塞在條件變量上,那么調(diào)用pthread_cond_signal()將沒有作用。
4)阻塞直到指定時(shí)間pthread_cond_timedwait
函數(shù)原型:int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mp, const structtimespec * abstime);?頭文件為:#include <time.h>
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤
參數(shù)說明:函數(shù)到了一定的時(shí)間,即使條件未發(fā)生也會(huì)解除阻塞。這個(gè)時(shí)間由參數(shù)abstime指定。函數(shù)返回時(shí),相應(yīng)的互斥鎖往往是鎖定的,即使是函數(shù)出錯(cuò)返回。
注意:pthread_cond_timedwait函數(shù)也是退出點(diǎn)。超時(shí)時(shí)間參數(shù)是指一天中的某個(gè)時(shí)刻。
使用舉例:
pthread_timestruc_t to; to.tv_sec = time(NULL) + TIMEOUT; to.tv_nsec = 0;
超時(shí)返回的錯(cuò)誤碼是ETIMEDOUT。
5)?釋放阻塞的所有線程pthread_cond_broadcast
函數(shù)原型:int pthread_cond_broadcast(pthread_cond_t *cv);
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤
參數(shù)說明:函數(shù)喚醒所有被pthread_cond_wait函數(shù)阻塞在某個(gè)條件變量上的線程,參數(shù)cv被用來指定這個(gè)條件變量。當(dāng)沒有線程阻塞在這個(gè)條件變量上時(shí),pthread_cond_broadcast函數(shù)無效。由于pthread_cond_broadcast函數(shù)喚醒所有阻塞在某個(gè)條件變量上的線程,這些線程被喚醒后將再次競(jìng)爭(zhēng)相應(yīng)的互斥鎖,所以必須小心使用pthread_cond_broadcast函數(shù)。
6)?釋放條件變量pthread_cond_destroy
函數(shù)原型:int pthread_cond_destroy(pthread_cond_t *cv);
返回值:函數(shù)成功返回0;任何其他返回值都表示錯(cuò)誤。
注意:條件變量占用的空間并未被釋放。
7)?喚醒丟失問題
在線程未獲得相應(yīng)的互斥鎖時(shí)調(diào)用pthread_cond_signal或pthread_cond_broadcast函數(shù)可能會(huì)引起喚醒丟失問題。
喚醒丟失往往會(huì)在下面的情況下發(fā)生:
一個(gè)線程調(diào)用pthread_cond_signal或pthread_cond_broadcast函數(shù);
另一個(gè)線程正處在測(cè)試條件變量和調(diào)用pthread_cond_wait函數(shù)之間;
沒有線程正在處在阻塞等待的狀態(tài)下。
3.舉例:
下面是使用函數(shù)pthread_cond_wait()和函數(shù)pthread_cond_signal()的一個(gè)簡(jiǎn)單的例子:
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count;
decrement_count () {
pthread_mutex_lock (&count_lock);
while(count==0)
pthread_cond_wait( &count_nonzero, &count_lock);
count=count -1;
pthread_mutex_unlock (&count_lock);
}
increment_count(){
pthread_mutex_lock(&count_lock);
if(count==0)
pthread_cond_signal(&count_nonzero);
count=count+1;
pthread_mutex_unlock(&count_lock);
}
count值為0時(shí),decrement函數(shù)在pthread_cond_wait處被阻塞,并打開互斥鎖count_lock。此時(shí),當(dāng)調(diào)用到函數(shù)increment_count時(shí),pthread_cond_signal()函數(shù)改變條件變量,告知decrement_count()停止阻塞。讀者可以試著讓兩個(gè)線程分別運(yùn)行這兩個(gè)函數(shù),看看會(huì)出現(xiàn)什么樣的結(jié)果。
函數(shù)pthread_cond_broadcast(pthread_cond_t *cond)用來喚醒所有被阻塞在條件變量cond上的線程。這些線程被喚醒后將再次競(jìng)爭(zhēng)相應(yīng)的互斥鎖,所以必須小心使用這個(gè)函數(shù)。
轉(zhuǎn)載于:https://www.cnblogs.com/chengxuyuancc/articles/3298854.html
總結(jié)
以上是生活随笔為你收集整理的多线程编程-条件变量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hadoop集群搭建(六):hadoop
- 下一篇: 一、数据与统计资料