Linux線程同步之條件變數

來源:互聯網
上載者:User

與互斥鎖不同,條件變數是用來等待而不是用來上鎖的。條件變數用來自動阻塞一個線程,直到某特殊情況發生為止通常條件變數和互斥鎖同時使用。

條件變數使我們可以睡眠等待某種條件出現。條件變數是利用線程間共用的全域變數進行同步的一種機制,主要包括兩個動作:一個線程等待"條件變數的條件成立"而掛起;另一個線程使"條件成立"(給出條件成立訊號)。

條件的檢測是在互斥鎖的保護下進行的。如果一個條件為假,一個線程自動阻塞,並釋放等待狀態改變的互斥鎖。如果另一個線程改變了條件,它發訊號給關聯的條件變數,喚醒一個或多個等待它的線程,重新獲得互斥鎖,重新評價條件。如果兩進程共用可讀寫的記憶體,條件變數可以被用來實現這兩進程間的線程同步。

使用條件變數之前要先進行初始化。可以在單個語句中產生和初始化一個條件變數如:pthread_cond_t my_condition=PTHREAD_COND_INITIALIZER;(用於進程間線程的通訊)。可以利用函數pthread_cond_init動態初始化。

條件變數分為兩部分: 條件和變數. 條件本身是由互斥量保護的. 線程在改變條件狀態前先要鎖住互斥量. 它利用線程間共用的全域變數進行同步的一種機制。

相關的函數如下:

1 int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr);   
 
2 int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex);
3 int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime);
4 int pthread_cond_destroy(pthread_cond_t *cond);
 
5 int pthread_cond_signal(pthread_cond_t *cond);
6 int pthread_cond_broadcast(pthread_cond_t *cond);
 //解除所有線程的阻塞簡要說明:           (1)初始化.init()或者pthread_cond_t cond=PTHREAD_COND_INITIALIER;屬性置為NULL      (2)等待條件成立.pthread_wait,pthread_timewait.wait()釋放鎖,並阻塞等待條件變數為真      timewait()設定等待時間,仍未signal,返回ETIMEOUT(加鎖保證只有一個線程wait)      (3)啟用條件變數:pthread_cond_signal,pthread_cond_broadcast(啟用所有等待線程)      (4)清除條件變數:destroy;無線程等待,否則返回EBUSY  詳細說明

1. 初始化:

    條件變數採用的資料類型是pthread_cond_t, 在使用之前必須要進行初始化, 這包括兩種方式:

  • 靜態: 可以把常量PTHREAD_COND_INITIALIZER給靜態分配的條件變數.
  • 動態: pthread_cond_init函數, 是釋放動態條件變數的記憶體空間之前, 要用pthread_cond_destroy對其進行清理.

#include <pthread.h>

int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);

成功則返回0, 出錯則返回錯誤編號.

    當pthread_cond_init的attr參數為NULL時, 會建立一個預設屬性的條件變數; 非預設情況以後討論.

2. 等待條件:

#include <pthread.h>

int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restric mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict timeout);

成功則返回0, 出錯則返回錯誤編號.

    這兩個函數分別是阻塞等待和逾時等待.

    等待條件函數等待條件變為真, 傳遞給pthread_cond_wait的互斥量對條件進行保護, 調用者把鎖住的互斥量傳遞給函數. 函數把調用線程放到等待條件的線程列表上, 然後對互斥量解鎖, 這兩個操作是原子的. 這樣便關閉了條件檢查和線程進入休眠狀態等待條件改變這兩個操作之間的時間通道, 這樣線程就不會錯過條件的任何變化.

    當pthread_cond_wait返回時, 互斥量再次被鎖住.

3. 通知條件:

#include <pthread.h>

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

成功則返回0, 出錯則返回錯誤編號.

    這兩個函數用於通知線程條件已經滿足. 調用這兩個函數, 也稱向線程或條件發送訊號. 必須注意, 一定要在改變條件狀態以後再給線程發送訊號.

樣本程式
#include <stdio.h>#include <pthread.h> pthread_mutex_t mutex;pthread_cond_t cond;void *thread1(void *arg) {pthread_cleanup_push(pthread_mutex_unlock, &mutex);    //提供函數回調保護    while (1) {printf("thread1 is running\n");pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("thread1 applied the condition\n");pthread_mutex_unlock(&mutex);sleep(4);}pthread_cleanup_pop(0);}void *thread2(void *arg) {while (1) {printf("thread2 is running\n");pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printf("thread2 applied the condition\n");pthread_mutex_unlock(&mutex);sleep(1);}}int main() {pthread_t thid1, thid2;printf("condition variable study!\n");pthread_mutex_init(&mutex, NULL);pthread_cond_init(&cond, NULL);pthread_create(&thid1, NULL, (void *) thread1, NULL);pthread_create(&thid2, NULL, (void *) thread2, NULL);do {pthread_cond_signal(&cond);} while (1);sleep(20);pthread_exit(0);return 0;}

 

條件變數與互斥鎖、訊號量的區別

       1.互斥鎖必須總是由給它上鎖的線程解鎖,訊號量的掛出即不必由執行過它的等待操作的同一進程執行。一個線程可以等待某個給定號誌,而另一個線程可以掛出該號誌。

       2.互斥鎖要麼鎖住,要麼被解開(二值狀態,類型二值訊號量)。

       3.由於訊號量有一個與之關聯的狀態(它的計數值),訊號量掛出操作總是被記住。然而當向一個條件變數發送訊號時,如果沒有線程等待在該條件變數上,那麼該訊號將丟失。

       4.互斥鎖是為了上鎖而設計的,條件變數是為了等待而設計的,號誌即可用於上鎖,也可用於等待,因而可能導致更多的開銷和更高的複雜性。

參考:http://blog.csdn.net/dai_weitao/archive/2007/08/22/1754964.aspx

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.