多線程編程: 條件變量

條件變量 條件變量是利用線程間共享的全局變量進行同步的一種機制, 主要包括兩個動做: 一個線程等待"條件變量的條件成立"而掛起; 另外一個線程使"條件成立"(給出條件成立信號). 爲了防止競爭,條件變量的使用老是和一個互斥鎖結合在一塊兒。
1. 建立和註銷 條件變量和互斥鎖同樣,都有靜態和動態兩種建立方式, 靜態方式使用PTHREAD_COND_INITIALIZER常量, 以下: pthread_cond_t cond = PTHREAD_COND_INITIALIZER
動態方式調用pthread_cond_init()函數,API定義以下: int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);
儘管POSIX標準中爲條件變量定義了屬性,但在LinuxThreads中沒有實現, 所以cond_attr值一般爲NULL,且被忽略.
註銷一個條件變量須要調用pthread_cond_destroy(), 只有在沒有線程在該條件變量上等待的時候才能註銷這個條件變量,不然返回EBUSY 由於Linux實現的條件變量沒有分配什麼資源,因此註銷動做只包括檢查是否有等待線程。 API定義以下: int pthread_cond_destroy(pthread_cond_t *cond);
2. 等待和激發
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);
等待條件有兩種方式: 無條件等待 pthread_cond_wait(); 計時等待 pthread_cond_timedwait(); 其中計時等待方式若是在給定時刻前條件沒有知足,則返回ETIMEOUT,結束等待. 其中abstime以與time()系統調用相贊成義的絕對時間形式出現,0表示格林尼治時間197011000
不管哪一種等待方式,都必須和一個互斥鎖配合, 以防止多個線程同時請求pthread_cond_wait()(pthread_cond_timedwait(),下同)的競爭條件(Race Condition).
mutex互斥鎖必須是普通鎖(PTHREAD_MUTEX_TIMED_NP)或者適應鎖(PTHREAD_MUTEX_ADAPTIVE_NP), 且在調用pthread_cond_wait()前必須由本線程加鎖(pthread_mutex_lock()), 而在更新條件等待隊列之前,mutex保持鎖定狀態,並在線程掛起進入等待前解鎖. 在條件知足從而離開pthread_cond_wait()以前,mutex將被從新加鎖,以與進入pthread_cond_wait()前的加鎖動做對應。
激發條件有兩種形式, pthread_cond_signal();
html

激活一個等待該條件的線程,存在多個等待線程時按入隊順序激活其中一個;node

 

pthread_cond_broadcast(); 激活全部等待線程。
如今來看一段典型的應用:看註釋便可。
併發

 

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>


static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

struct node 
{
int n_number;
struct node *n_next;
} *head = NULL;

static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread.\n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}

static void *thread_func(void *arg)
{
struct node *p = NULL;


pthread_cleanup_push(cleanup_handler, p);
while (1) 
{
pthread_mutex_lock(&mtx); //這個mutex主要是用來保證pthread_cond_wait的併發性
while (head == NULL) 
{ 
/* 
* 這個while要特別說明一下,
* 單個pthread_cond_wait功能很完善,爲什麼這裏要有一個while (head == NULL)呢?
* 由於pthread_cond_wait裏的線程可能會被意外喚醒,若是這個時候head != NULL,則不是咱們想要的狀況。
* 這個時候,應該讓線程繼續進入pthread_cond_wait
*/

/*
* pthread_cond_wait會先解除以前的pthread_mutex_lock鎖定的mtx,
* 而後阻塞在等待對列裏休眠,
* 直到再次被喚醒(大多數狀況下是等待的條件成立而被喚醒,喚醒後,該線程會先鎖定pthread_mutex_lock(&mtx);
* 再讀取資源;
* 這個流程能夠表述爲:block-->unlock-->wait() return-->lock.
*/
pthread_cond_wait(&cond, &mtx);
}


p = head;
head = head->n_next;
printf("Got %d from front of queue\n", p->n_number);


free(p);
pthread_mutex_unlock(&mtx); //臨界區數據操做完畢,釋放互斥鎖
}

pthread_cleanup_pop(0);
return 0;
}

int main(void)
{
pthread_t tid;
int i;
struct node *p;

/*
* 子線程會一直等待資源,相似生產者和消費者,
* 可是這裏的消費者能夠是多個消費者,而不單單支持普通的單個消費者;
* 這個模型雖然簡單,可是很強大
*/
pthread_create(&tid, NULL, thread_func, NULL); 

for (i = 0; i < 10; i++) 
{
p = malloc(sizeof(struct node));
p->n_number = i;


pthread_mutex_lock(&mtx); //須要操做head這個臨界資源,先加鎖,

p->n_next = head;
head = p;

pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解鎖

sleep(1);
}

printf("thread 1 wanna end the line.So cancel thread 2./n");

/*
* 關於pthread_cancel, 有一點額外的說明,
* 它是從外部終止子線程,子線程會在最近的取消點,退出線程;
* 而在咱們的代碼裏,最近的取消點確定就是pthread_cond_wait()了。
* 關於取消點的信息,有興趣能夠google,這裏很少說了
*/
pthread_cancel(tid);
pthread_join(tid, NULL);

printf("All done -- exiting/n");
return 0;
}

 

轉:http://blog.chinaunix.net/uid-26000296-id-3484910.html函數

相關文章
相關標籤/搜索