Linux中,pthread_cond_t, 条件变量的使用举例

LINUX系统编程这一快的知识,如果不经常使用的话,可能也会忘记,在项目中,可以代码可以看懂,但是自己亲自写的话,可能还是要翻一下<Linux 环境高级编程>这本书比较好,下面就简单记一下这个知识点

一般pthread_cond_t,会搭配pthread_mutex_t 一起使用的, 因为线程间通信时操作共享内存时,需要用到锁。当锁住的共享变量发生改变时,可能需要通知相应的线程(因为可能该共享变量涉及到多个线程),这时就需要用到pthread_cond_t这种条件变量来精准的通知某个或几个线程, 让他们执行相应的操作

pthread_cond_t涉及两个函数,一个是pthread_cond_signal函数,它在一个线程中,用来发送信号。一个是pthread_cond_wait函数,他在另一个线程中,用来接收信号。当线程执行到pthread_cond_wait函数时,他会释放相应的锁,让其他线程获得锁继续执行,这样其他线程才有机会给他发信号;当它接收到信号时,会重新去获得锁,如果没有获得锁,就阻塞等待,直到获得锁,才执行接收信号的相应操作。

废话这么多,直接上代码例子

#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++)
{
printf("djh: Line: %d, i = %d\n", __LINE__, 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__);
printf("%s will sleep 1s in Line: %d \n", __FUNCTION__, __LINE__);
sleep(1);

}
pthread_mutex_unlock(&mutex);/*解锁互斥量*/
printf("thread1: unlock %d\n", __LINE__);
printf("%s will sleep 1s in Line: %d \n\n", __FUNCTION__, __LINE__);
sleep(1);

}
}


void *thread2(void *junk)
{
while(i<6)
while(i<6)
{
printf("djh: Line: %d, i = %d\n", __LINE__, i);
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", __LINE__);
printf("%s will sleep 1s in Line: %d \n\n", __FUNCTION__, __LINE__);
sleep(1);
}
}

编译:

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

以下是程序运行结果:

djh: Line: 29, i = 1
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


djh: Line: 53, i = 1
thread2: lock 55
thread2: wait 1 57
djh: Line: 29, i = 2
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


djh: Line: 29, i = 3
thread1: lock 31
thread1:signal 1 33
thread1:signal 2 35
thread1 will sleep 1s in Line: 36 
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


thread2: wait 2 59
thread2: unlock 62
thread2 will sleep 1s in Line: 63 


djh: Line: 29, i = 4
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


djh: Line: 53, i = 4
thread2: lock 55
thread2: wait 1 57
djh: Line: 29, i = 5
thread1: lock 31
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


djh: Line: 29, i = 6
thread1: lock 31
thread1:signal 1 33
thread1:signal 2 35
thread1 will sleep 1s in Line: 36 
thread1: unlock 41
thread1 will sleep 1s in Line: 42 


thread2: wait 2 59
thread2: unlock 62
thread2 will sleep 1s in Line: 63

这里的两个关键函数就在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锁,进入下一个循环。


  • 12
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值