LINUX環境下多線程編程確定會遇到須要條件變量的狀況,此時必然要使用pthread_cond_wait()函數。但這個函數的執行過程比較難於理解。
pthread_cond_wait()的工做流程以下(以MAN中的EXAMPLE爲例):
Consider two shared variables x and y, protected by the mutex mut, and a condition vari-
able cond that is to be signaled whenever x becomes greater than y.html
int x,y;
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;linux
Waiting until x is greater than y is performed as follows:編程
pthread_mutex_lock(&mut);
while (x <= y) {
pthread_cond_wait(&cond, &mut);
}
/* operate on x and y */
pthread_mutex_unlock(&mut);多線程
Modifications on x and y that may cause x to become greater than y should signal the con-
dition if needed:app
pthread_mutex_lock(&mut);
/* modify x and y */
if (x > y) pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mut);ide
這個例子的意思是,兩個線程要修改X和 Y的值,第一個線程當X<=Y時就掛起,直到X>Y時才繼續執行(由第二個線程可能會修改X,Y的值,當X>Y時喚醒第一個線程),即 首先初始化一個普通互斥量mut和一個條件變量cond。以後分別在兩個線程中分別執行以下函數體:函數
pthread_mutex_lock(&mut);
while (x <= y) {
pthread_cond_wait(&cond, &mut);
}
/* operate on x and y */
pthread_mutex_unlock(&mut);oop
和: pthread_mutex_lock(&mut);
/* modify x and y */
if (x > y) pthread_cond_signal(&cond);
pthread_mutex_unlock(&mut);
其實函數的執行過程很是簡單,在第一個線程執行到pthread_cond_wait(&cond,&mut)時,此時若是X<=Y,則此函數就將mut互斥量解鎖 ,再將cond條件變量加鎖 ,此時第一個線程掛起 (不佔用任何CPU週期)。
而在第二個線程中,原本由於mut被第一個線程鎖住而阻塞,此時由於mut已經釋放,因此能夠得到鎖mut,而且進行修改X和Y的值,在修改以後,一個IF語句斷定是否是X>Y,若是是,則此時pthread_cond_signal()函數會喚醒第一個線程 ,並在下一句中釋放互斥量mut。而後第一個線程開始從pthread_cond_wait()執行,首先要再次鎖mut , 若是鎖成功,再進行條件的判斷 (至於爲何用WHILE,即在被喚醒以後還要再判斷,後面有緣由分析),若是知足條件,則被喚醒 進行處理,最後釋放互斥量mut 。性能
至於爲何在被喚醒以後還要再次進行條件判斷(即爲何要使用while循環來判斷條件),是由於可能有「驚羣效應」。有人以爲此處既然是被喚醒的,確定 是知足條件了,其實否則。若是是多個線程都在等待這個條件,而同時只能有一個線程進行處理,此時就必需要再次條件判斷,以使只有一個線程進入臨界區處理。 對此,轉來一段:ui
引用下POSIX的RATIONALE:
Condition Wait Semantics
It is important to note that when pthread_cond_wait() and pthread_cond_timedwait() return without error, the associated predicate may still be false. Similarly, when pthread_cond_timedwait() returns with the timeout error, the associated predicate may be true due to an unavoidable race between the expiration of the timeout and the predicate state change.
The application needs to recheck the predicate on any return because it cannot be sure there is another thread waiting on the thread to handle the signal, and if there is not then the signal is lost. The burden is on the application to check the predicate.
Some implementations, particularly on a multi-processor, may sometimes cause multiple threads to wake up when the condition variable is signaled simultaneously on different processors.
In general, whenever a condition wait returns, the thread has to re-evaluate the predicate associated with the condition wait to determine whether it can safely proceed, should wait again, or should declare a timeout. A return from the wait does not imply that the associated predicate is either true or false.
It is thus recommended that a condition wait be enclosed in the equivalent of a "while loop" that checks the predicate.
從上文能夠看出:
1,pthread_cond_signal在多處理器上可能同時喚醒多個線程,當你只能讓一個線程處理某個任務時,其它被喚醒的線程就須要繼續 wait,while循環的意義就體如今這裏了,並且規範要求pthread_cond_signal至少喚醒一個pthread_cond_wait上 的線程,其實有些實現爲了簡單在單處理器上也會喚醒多個線程.
2,某些應用,如線程池,pthread_cond_broadcast喚醒所有線程,但咱們一般只須要一部分線程去作執行任務,因此其它的線程須要繼續wait.因此強烈推薦此處使用while循環.
其實說白了很簡單,就是pthread_cond_signal()也可能喚醒多個線程,而若是你同時只容許一個線程訪問的話,就必需要使用while來進行條件判斷,以保證臨界區內只有一個線程在處理。
==============================另外一篇很好的文章===========================
==============================使用效率問題============================