pthread_cond_broadcast与pthread_cond_signal的区别——linuxc

pthread_cond_signal——唤醒睡眠的线程,一次只能唤醒一个线程
pthread_cond_broadcast——唤醒睡眠的线程,一次唤醒所有睡眠的线程

demon1——pthread_cond_signal

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

static pthread_t thread1;
static pthread_t thread2;

//静态初始化
static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void *function1()

{
	while(1)
	{
		pthread_mutex_lock(&lock);
		printf("=====  线程1进入睡眠        ====\n");
		pthread_cond_wait(&cond,&lock);
		printf("====  线程1    唤醒    ====\n");
		pthread_mutex_unlock(&lock);
	}
}

void *function2()

{
	while(1)
	{
		pthread_mutex_lock(&lock);
		printf("=====  线程2进入睡眠        ====\n");
		pthread_cond_wait(&cond,&lock);
		printf("====  线程2    唤醒    ====\n");
		pthread_mutex_unlock(&lock);
	}
}


int main()
{
	int i=0;
	if(-1==pthread_create(&thread1,NULL,function1,NULL))
	{
		printf("thread_create1 fail!\n");
		pthread_detach(thread1);
	}

	if(-1==pthread_create(&thread2,NULL,function2,NULL))
	{
		printf("thread_create fail!\n");
		pthread_detach(thread1);
	}

	while(1)
	{
		sleep(2);
		i++;
		printf("\n 第%d次唤醒\n",i);
		pthread_mutex_lock(&lock);
		if(-1==pthread_cond_signal(&cond))
		{
			printf("pthread_cond_broadcast error!\n");
		}
		pthread_mutex_unlock(&lock);
	}
	return 0;
}

运行结果:
在这里插入图片描述
demon2——pthread_cond_broadcast

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

static pthread_t thread1;
static pthread_t thread2;

//静态初始化
static pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;

void *function1()

{
	while(1)
	{
		pthread_mutex_lock(&lock);
		printf("=====  线程1进入睡眠        ====\n");
		pthread_cond_wait(&cond,&lock);
		printf("====  线程1    唤醒    ====\n");
		pthread_mutex_unlock(&lock);
	}
}

void *function2()

{
	while(1)
	{
		pthread_mutex_lock(&lock);
		printf("=====  线程2进入睡眠        ====\n");
		pthread_cond_wait(&cond,&lock);
		printf("====  线程2    唤醒    ====\n");
		pthread_mutex_unlock(&lock);
	}
}


int main()
{
	int i=0;
	if(-1==pthread_create(&thread1,NULL,function1,NULL))
	{
		printf("thread_create1 fail!\n");
		pthread_detach(thread1);
	}

	if(-1==pthread_create(&thread2,NULL,function2,NULL))
	{
		printf("thread_create fail!\n");
		pthread_detach(thread1);
	}

	while(1)
	{
		sleep(2);
		i++;
		printf("\n 第%d次唤醒\n",i);
		pthread_mutex_lock(&lock);
		if(-1==pthread_cond_broadcast(&cond))
		{
			printf("pthread_cond_broadcast error!\n");
		}
		pthread_mutex_unlock(&lock);
	}
	return 0;
}

运行结果:
在这里插入图片描述
对比运行结果,清楚的可以发现pthread_cond_broadcast可以唤醒所有睡眠的线程

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`pthread_cond_wait` 是 Linux 下的一个线程同步原语,用于在多线程并发环境下实现线程之间的等待和唤醒机制。 具体来说,`pthread_cond_wait` 一般结合互斥锁(`pthread_mutex`)使用,用于实现线程的条件等待。一个线程在调用 `pthread_cond_wait` 函数时,会先释放持有的互斥锁,然后进入等待状态,直到另一个线程调用 `pthread_cond_signal` 或 `pthread_cond_broadcast` 函数将其唤醒。当线程被唤醒后,它会重新获取之前释放的互斥锁,并从 `pthread_cond_wait` 函数返回。 `pthread_cond_wait` 的函数原型如下: ```c int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); ``` 其中,`cond` 是一个条件变量,`mutex` 是一个互斥锁。在调用 `pthread_cond_wait` 函数之前,一般需要先加锁 `mutex`,然后再调用 `pthread_cond_wait`。`pthread_cond_wait` 函数会自动释放 `mutex` 并将线程置于等待状态,直到另一个线程调用 `pthread_cond_signal` 或 `pthread_cond_broadcast` 函数将其唤醒。唤醒后,`pthread_cond_wait` 函数会自动重新获取 `mutex` 并从函数返回。 需要注意的是,`pthread_cond_wait` 函数可能会出现虚假唤醒的情况,即线程在没有收到信号的情况下被唤醒。因此,一般需要在使用 `pthread_cond_wait` 函数时加上一个循环条件,以确保线程被正确地唤醒。例如: ```c pthread_mutex_lock(&mutex); while (condition == 0) { pthread_cond_wait(&cond, &mutex); } // 线程被唤醒后继续执行 pthread_mutex_unlock(&mutex); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值