多线程同步条件变量(转载)
最近看《UNIX环境高级编程》多线程同步,看到他举例说条件变量pthread_cond_t怎么用,愣是没有看懂,只好在网上找了份代码,跑了跑,才弄明白
- #include <pthread.h>
- #include <stdio.h>
- #include <stdlib.h>
- pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/
- pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*初始化条件变量*/
- void *thread1(void *);
- void *thread2(void *);
- int i=1;
- int main(void)
- {
- pthread_t t_a;
- pthread_t t_b;
- pthread_create(&t_a,NULL,thread1,(void *)NULL);/*创建进程t_a*/
- pthread_create(&t_b,NULL,thread2,(void *)NULL); /*创建进程t_b*/
- pthread_join(t_a, NULL);/*等待进程t_a结束*/
- pthread_join(t_b, NULL);/*等待进程t_b结束*/
- pthread_mutex_destroy(&mutex);
- pthread_cond_destroy(&cond);
- exit(0);
- }
- void *thread1(void *junk)
- {
- for(i=1;i<=6;i++)
- {
- pthread_mutex_lock(&mutex);/*锁住互斥量*/
- printf("thread1: lock %d/n", __LINE__);
- if(i%3==0){
- printf("thread1:signal 1 %d/n", __LINE__);
- pthread_cond_signal(&cond);/*条件改变,发送信号,通知t_b进程*/
- printf("thread1:signal 2 %d/n", __LINE__);
- sleep(1);
- }
- pthread_mutex_unlock(&mutex);/*解锁互斥量*/
- printf("thread1: unlock %d/n/n", __LINE__);
- sleep(1);
- }
- }
- void *thread2(void *junk)
- {
- while(i<6)
- {
- pthread_mutex_lock(&mutex);
- printf("thread2: lock %d/n", __LINE__);
- if(i%3!=0){
- printf("thread2: wait 1 %d/n", __LINE__);
- pthread_cond_wait(&cond,&mutex);/*解锁mutex,并等待cond改变*/
- printf("thread2: wait 2 %d/n", __LINE__);
- }
- pthread_mutex_unlock(&mutex);
- printf("thread2: unlock %d/n/n", __LINE__);
- sleep(1);
- }
- }
编译:
[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锁,进入下一个循环。