pthread_cond_broadcast&pthread_cond_signal使用

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

static pthread_cond_t cond;
static pthread_mutex_t mutex1;
static pthread_mutex_t mutex2;
void *thread1_entry(void *arg)
{
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond wait cond1\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("recv cond1\n");
                sleep(1);
                pthread_mutex_unlock(&mutex1);

        }
}

void *thread2_entry(void *arg)
{
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond wait cond2\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("recv cond2\n");
                sleep(10);
                pthread_mutex_unlock(&mutex1);
        }

}

void *thread3_entry(void *arg)
{
        int ret;
        while(1)
        {
                pthread_mutex_lock(&mutex1);
               
                ret = pthread_cond_broadcast(&cond);
                if(ret < 0)
                {
                        printf("pthread_cond_broadcast error\n");
                }
                
                pthread_mutex_unlock(&mutex1);
                sleep(2);
        }
}
int main(void)
{
        int ret =0;
        pthread_t tid1, tid2, tid3;
        ret = pthread_cond_init(&cond, NULL);
        if(ret < 0)
        {
                printf("pthread_cond_init error\n");
        }
        ret = pthread_mutex_init(&mutex1, NULL);
        if(ret < 0)
        {
                printf("pthread_mutex_init error\n");
        }

        ret = pthread_mutex_init(&mutex2,NULL);
        if(ret < 0)
        {
                printf("pthread_mutex_init error\n");
        }

        ret=  pthread_create(&tid1, NULL, thread1_entry, NULL);
        if(ret < 0)
        {
                printf("pthread_create thread1 error\n");
        }

        ret = pthread_create(&tid2, NULL, thread2_entry, NULL);
        if(ret < 0)
        {
                printf("pthread_create thread2 error\n");
        }
        sleep(2);
        ret = pthread_create(&tid3, NULL, thread3_entry, NULL);
        if(ret < 0)
        {
                printf("pthread_create thread3 error\n");
        }

        while(1)
        {
                sleep(10000);
        }
        return 0;
}

这段代码由于用了同一个锁,两个线程thread1和thread2是顺序执行,如果把thread2更改成用不同的锁,如下

void *thread2_entry(void *arg)
{
        while(1)
        {
                pthread_mutex_lock(&mutex2);
                printf("cond wait cond2\n");
                pthread_cond_wait(&cond, &mutex2);
                printf("recv cond2\n");
                sleep(10);
                
                pthread_mutex_unlock(&mutex2);
        }
}

两个线程的运行是没有关系的, 并行的。

1.pthread_cond_wait 前后需要加锁,pthread_cond_broadcast前后并不一定要加锁

2.pthread_cond_broadcast能同时唤醒多个在等待的线程,pthread_cond_signal一次只能唤醒一个

https://blog.csdn.net/pengrui18/article/details/70556269

https://www.cnblogs.com/XiaoXiaoShuai-/p/11855408.html

https://blog.csdn.net/yeyuangen/article/details/37593533

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值