pthread_cond_broadcast使用示例

今天在使用pthread_cond_t时,发现pthread_cond_t使用pthread_cond_broadcast函数唤醒多个条件变量时,使用两个互斥量分别控制时,只能唤醒其中一个变量,最后通过测试发现只能使用一个互斥量去控制。
个人理解:
thread1中使用pthread_mutex_lock(&mutex1);加互斥所,然后在pthread_cond_wait(&cond, &mutex1);这样可以保证这期间没有pthread_cond_broadcast或者pthread_cond_signal操作,从而保证条件不会丢失,当pthread_cond_wait(&cond, &mutex1);
完成操作之后,会自动释放互斥量。
然后thread2中的互斥量也紧接着根据上述方式加入等待对列,当调用pthread_cond_broadcast时会根据加入等待队列中的先后顺序依次唤醒他们。当是还是不明白为什么使用两个互斥量就只能唤醒其中一个呢?有清楚的朋友可以留言。

#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");
                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");
                pthread_mutex_unlock(&mutex1);
        }

}

void *thread3_entry(void *arg)
{
        int ret;
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                pthread_mutex_lock(&mutex2);
                ret = pthread_cond_broadcast(&cond);
                if(ret < 0)
                {
                        printf("pthread_cond_broadcast error\n");
                }
                pthread_mutex_unlock(&mutex2);
                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");
        }
#if 0
        pthread_mutex_lock(&mutex1);
        pthread_mutex_lock(&mutex2);
        ret = pthread_cond_broadcast(&cond);
        if(ret < 0)
        {
                printf("pthread_cond_broadcast error\n");
        }
        pthread_mutex_unlock(&mutex1);
        pthread_mutex_unlock(&mutex2);
#endif
        while(1)
        {
                sleep(10000);
        }
        return 0;
}
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值