首页 > 代码库 > 条件变量pthread_cond_t怎么用

条件变量pthread_cond_t怎么用

 1 #include <pthread.h>   2 #include <stdio.h>   3 #include <stdlib.h>   4 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/   5 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/   6 void *thread1(void *);   7 void *thread2(void *);   8 int i=1;   9 int main(void)  10 {  11     pthread_t t_a;  12     pthread_t t_b;  13     pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/  14     pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/  15     pthread_join(t_a, NULL);/*等待进程t_a结束*/  16     pthread_join(t_b, NULL);/*等待进程t_b结束*/  17     pthread_mutex_destroy(&mutex);  18     pthread_cond_destroy(&cond);  19     exit(0);  20 }  21 void *thread1(void *junk)  22 {  23     for(i=1;i<=6;i++)  24     {  25         pthread_mutex_lock(&mutex);/*锁住互斥量*/  26         printf("thread1: lock %d/n", __LINE__);  27         if(i%3==0){  28             printf("thread1:signal 1  %d/n", __LINE__);  29             pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/  30             printf("thread1:signal 2  %d/n", __LINE__);  31             sleep(1);  32         }  33         pthread_mutex_unlock(&mutex);/*解锁互斥量*/  34         printf("thread1: unlock %d/n/n", __LINE__);  35         sleep(1);  36     }  37 }  38 void *thread2(void *junk)  39 {  40     while(i<6)  41     {  42         pthread_mutex_lock(&mutex);  43         printf("thread2: lock %d/n", __LINE__);  44         if(i%3!=0){  45             printf("thread2: wait 1  %d/n", __LINE__);  46             pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/  47             printf("thread2: wait 2  %d/n", __LINE__);  48         }  49         pthread_mutex_unlock(&mutex);  50         printf("thread2: unlock %d/n/n", __LINE__);  51         sleep(1);  52     }  53 }  

编译:

[X61@horizon threads]$ gcc thread_cond.c -lpthread -o tcd

 

以下是程序运行结果:

[X61@horizon threads]$ ./tcd 
thread1: lock 30
thread1: unlock 40

thread2: lock 52
thread2: wait 1 55
thread1: lock 30
thread1: unlock 40

thread1: lock 30
thread1:signal 1 33
thread1:signal 2 35
thread1: unlock 40

thread2: wait 2 57
thread2: unlock 61

thread1: lock 30
thread1: unlock 40

thread2: lock 52
thread2: wait 1 55
thread1: lock 30
thread1: unlock 40

thread1: lock 30
thread1:signal 1 33
thread1:signal 2 35
thread1: unlock 40

thread2: wait 2 57
thread2: unlock 61

 

这里的两个关键函数就在pthread_cond_wait和pthread_cond_signal函数。

本例中:

线程一先执行,获得mutex锁,打印,然后释放mutex锁,然后阻塞自己1秒。

线程二此时和线程一应该是并发的执行
 ,这里是一个要点,为什么说是线程此时是并发的执行,因为此时不做任何干涉的话,是没有办法确定是线程一先获得执行还是线程二先获得执行,到底那个线程先获得执行,取决于操作系统的调度,想刻意的让线程2先执行,可以让线程2一出来,先sleep一秒。
这里并发执行的情况是,线程一先进入循环,然后获得锁,此时估计线程二执行,阻塞在
pthread_mutex_lock(&mutex);
这行语句中,直到线程1释放mutex锁
pthread_mutex_unlock(&mutex);/*解锁互斥量*/
然后线程二得已执行,获取metux锁,满足if条件,到pthread_cond_wait (&cond,&mutex);/*等待*/
这里的线程二阻塞,不仅仅是等待cond变量发生改变,同时释放mutex锁 ,因为当时看书没有注意,所以这里卡了很久。
mutex锁释放后,线程1终于获得了mutex锁,得已继续运行,当线程1的if(i%3==0)的条件满足后,通过pthread_cond_signal发送信号,告诉等待cond的变量的线程(这个情景中是线程二),cond条件变量已经发生了改变。

不过此时线程二并没有立即得到运行 ,因为线程二还在等待mutex锁的释放,所以线程一继续往下走,直到线程一释放mutex锁,线程二才能停止等待,打印语句,然后往下走通过pthread_mutex_unlock(&mutex)释放mutex锁,进入下一个循环。

 

转自:http://blog.csdn.net/zclongembedded/article/details/7337729

条件变量pthread_cond_t怎么用