Linux多線程編程(二)
引言
條件變量是利用線程間共享的全局變量進行同步的一種機制,主要包括兩個動作:一個線程等待條件變量的條件成立而掛起(此時不再占用cpu);另一個線程使條件成立(給出條件成立信號)。為了防止競爭,條件變量的使用總是和一個互斥鎖結(jié)合在一起。
函數(shù)原型
1. 定義條件變量
#include <pthread.h> /* 定義兩個條件變量 */ pthread_cond_t cond_pro, cond_con;
2. 初始化和銷毀條件變量
#include <pthread.h> int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond); /* 初始化條件變量 */ pthread_cond_init(&cond_pro, NULL); pthread_cond_init(&cond_con, NULL); /* 銷毀條件變量 */ pthread_cond_destroy(&cond_pro); pthread_cond_destroy(&cond_pro);
3. 等待和激發(fā)條件
#include <pthread.h> int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex); int pthread_cond_broadcast(pthread_cond_t *cond); int pthread_cond_signal(pthread_cond_t *cond); /* 等待條件 */ /* 注意:pthread_cond_wait為阻塞函數(shù)。解開鎖,再等待。等條件滿足時,需要搶到鎖,才可以被喚醒*/ pthread_cond_wait(&cond_pro,&mutex); /* 激發(fā)條件 */ /* 所有因為不滿足條件的線程都會阻塞在條件變量cond_pro中的一個隊列中 */ /* 以廣播方式,通知所有被阻塞的所有線程 */ pthread_cond_broadcast(&cond_pro); /* 以signal方式,只通知排在最前面的線程 */ pthread_cond_signal(&cond_pro);
代碼
/*************************************************************************
> File Name: my_con.c
> Author: KrisChou
> Mail:zhoujx0219@163.com
> Created Time: Tue 26 Aug 2014 10:24:29 AM CST
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#define CELL 10
#define FLORE 0
int i = 0; /* 所有線程共享的全局變量,此處假定至多遞增至10,最小減到0 */
pthread_mutex_t mutex; /* 定義互斥鎖 */
pthread_cond_t cond_pro, cond_con; /* 定義兩個條件變量 */
/* 生產(chǎn)者線程 */
void* pro_handler(void *arg)
{
pthread_detach(pthread_self()); /* 由系統(tǒng)回收線程資源,而非主線程回收資源 ,此類情況主線程是個服務器,永久不會退出 */
while(1)
{
pthread_mutex_lock(&mutex);
while(i >= CELL)
{
pthread_cond_wait(&cond_pro,&mutex);
/* continue是輪詢,此處是阻塞 */
/* 把鎖放開再等 ,第一個參數(shù)是結(jié)構(gòu)體指針,其中有成員存放被阻塞的函數(shù) */
/*不占cpu*/
/* 不滿足條件時才會等 ,需要別人告訴它,才能喚醒它*//* 當它返回時,鎖也要回來了*/
}
i++;
if(i == 1)
{
/* 由空到不空,喚醒消費者 */
pthread_cond_signal(&cond_con); /*不會立馬signal被阻塞的消費者線程,因為其還要等鎖搶回來*/
}
printf("add i: %d \n", i);
pthread_mutex_unlock(&mutex);
sleep(rand() % 5 + 1);
}
}
/* 消費者線程 */
void* con_handler(void *arg)
{
pthread_detach(pthread_self());
while(1)
{
pthread_mutex_lock(&mutex);
while(i <= FLORE)
{
pthread_cond_wait(&cond_cno,&mutex);
}
i--;
if(i == 9) /* 由滿到不滿,要告訴生產(chǎn)者,以便將其喚醒 *//*此處,直接signal也可以,我們是為了更加精確*/
{
pthread_cond_signal(&cond_pro);
}
printf("con i: %d \n", i);
pthread_mutex_unlock(&mutex);
sleep(rand() % 5 + 1);
}
}
int main(int argc, char *argv[]) // exe +num -num
{
srand(getpid());
int con_cnt, pro_cnt;
pro_cnt = atoi(argv[1]);
con_cnt = atoi(argv[2]);
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond_pro,NULL);
pthread_cond_init(&cond_con,NULL);
pthread_t *arr = (pthread_t*)calloc(con_cnt + pro_cnt , sizeof(pthread_t));
int index = 0;
while(pro_cnt > 0)
{
pthread_create(arr + index, NULL, pro_handler, NULL);
index++;
pro_cnt--;
}
while(con_cnt > 0)
{
pthread_create(arr + index, NULL, con_handler, NULL);
index++;
con_cnt--;
}
while(1);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond_pro);
pthread_cond_destroy(&cond_con);
return 0;
}
注意
無論是在生產(chǎn)者線程,還是在消費者線程中。標記黃色部分的判斷條件必須用while。以生產(chǎn)者線程舉例,當i>=CELL時,也就是i滿時,此時執(zhí)行pthread_cond_wait(&cond_cno,&mutex); 該生產(chǎn)者線程被掛起。必須等到消費者線程pthread_cond_signal(&cond_pro); 將其喚醒。但是消費者將其signal還不夠,被掛其的生產(chǎn)者線程必須重新拿到鎖,才可以被激活。但是,由于在消費者signal的同時,生產(chǎn)者并不能立即搶到鎖,所以此時可能i值又改變變?yōu)榇笥诘扔?0了。因此必須用while。不然可能導致i>10。

