pthread的条件变量:pthread_cond_wait

在阅读mjpg-streamer源码时,看到input_uvc.c中,在input_run()函数中会创建一个摄像头图像捕获线程cam_thread(),cam_thread中循环通过select判断摄像头文件再读取帧,读到帧数据后广播发送条件变量消息。

...
pthread_mutex_lock(&pglobal->in[pcontext->id].db);
...
pthread_cond_broadcast(&pglobal->in[pcontext->id].db_update);
pthread_mutex_unlock(&pglobal->in[pcontext->id].db);

 db和db_update是输入插件数组中的互斥锁和条件变量。而在输出插件output_udp的函数worker_thread()循环等待输入插件的条件变量。

pthread_mutex_lock(&pglobal->in[input_number].db);
pthread_cond_wait(&pglobal->in[input_number].db_update, &pglobal->in[input_number].db);
...
pthread_mutex_unlock(&pglobal->in[input_number].db);

当时有点疑惑,函数pthread_mutex_lock(mutex)是线程获得互斥锁mutex,如果mutex没被上锁函数立即返回则本线程占有互斥锁,否则线程阻塞等待;pthread_cond_wait(&cond,&mutex)是阻塞等待条件变量cond变化,分属不同线程的pthread_cond_waitpthread_cond_broadcast都要pthread_mutex_lock占用mutex,这样会不会导致死锁?

通过网上查找资料得到了答案:pthread_cond_wait()做的第一件事就是对互斥锁mutex解锁,并等待cond发生,线程休眠,此时其他线程可以获得mutex,一旦某线程获得mutex然后调用pthread_cond_broadcast()广播cond消息,等待cond而休眠的线程会唤醒(此时该线程还在pthread_cond_wait()之中没有返回),pthread_cond_wait最后会把mutex重新上锁然后返回。

为了验证一下,在网上看到一个测试程序比较好,拿来做了一点修改。

 ******************分割线 ************************

#include <stdio.h>
#include <pthread.h>
//gcc broadcast.c -o broadcast -lpthread
static pthread_cond_t cond;
static pthread_mutex_t mutex1;
static int count;
//static pthread_mutex_t mutex2;
void *thread1_entry(void *arg)
{
	//sleep(2);
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond1\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("t1 c = %d\n",count);
                pthread_mutex_unlock(&mutex1);

        }
}

void *thread2_entry(void *arg)
{
	//sleep(2);
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond2\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("t2 c = %d\n",count);
                pthread_mutex_unlock(&mutex1);
        }

}
void *thread3_entry(void *arg)
{
	//sleep(2);
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond3\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("t3 c = %d\n",count);
                pthread_mutex_unlock(&mutex1);

        }
}

void *thread4_entry(void *arg)
{
	//sleep(2);
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                printf("cond4\n");
                pthread_cond_wait(&cond, &mutex1);
                printf("t4 c = %d\n",count);
                pthread_mutex_unlock(&mutex1);
        }

}

void *thread5_entry(void *arg)
{
        int ret;
		count = 0;
        while(1)
        {
                pthread_mutex_lock(&mutex1);
                //pthread_mutex_lock(&mutex2);
				printf("broadcast\n");
				count++;
                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, tid4, tid5;
        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");
        }

        pthread_create(&tid1, NULL, thread1_entry, NULL);
        pthread_create(&tid2, NULL, thread2_entry, NULL);
        pthread_create(&tid3, NULL, thread3_entry, NULL);
        pthread_create(&tid4, NULL, thread4_entry, NULL);
        sleep(2);
        ret = pthread_create(&tid5, NULL, thread5_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;
}

编译:gcc broadcast.c -o broadcast -lpthread

./broadcast

运行结果:

cond1
cond3
cond2
cond4
broadcast
t1 c = 1
cond1
t2 c = 1
cond2
t3 c = 1
cond3
t4 c = 1
cond4
broadcast
t3 c = 2
cond3
t2 c = 2
cond2
t4 c = 2
t1 c = 2
cond1
cond4
broadcast
t3 c = 3
cond3
t2 c = 3
cond2
t1 c = 3
cond1
t4 c = 3
cond4
broadcast
t3 c = 4
cond3
t2 c = 4
cond2
t1 c = 4
cond1
t4 c = 4
cond4
broadcast
t3 c = 5
cond3
t2 c = 5
cond2
t1 c = 5
cond1
t4 c = 5
cond4

这个测试的思路就是有4个线程等待同一个条件变量,条件满足后打印变量count;第5个线程发送条件变量消息并自增count。可以看到运行结果,线程1调用pthread_cond_wait函数后会解锁mutex1,这样线程2、3、4就能获得mutex1并调用pthread_cond_wait等待,线程5也获得mutex1并发送了条件满足的消息,打印出count值能够判断其他4个线程收到的是不是同一个消息。

总结:由此可以知道,output_udp的线程会阻塞等待input_uvc的条件变量满足,input_uvc之所以用pthread_cond_broadcast而不是pthread_cond_signal是因为mjpg-streamer里一个input可以同时对应N个output。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值