C多线程:一生产者与多消费者

C多线程:一生产者与多消费者

/**
本例程是创建一个线程作为生产者,三个线程作为消费者
要求:
	a)为解决供多于求的情况,生产者一次只能生产一个,并等待消费者获取再生产下一个
	b)消费者们防止资源竞争,不能同时获取一个数,即前一个消费者获取了一个数,下一个消费者要等待下一个数的产生	   才可以获取

实现:加锁 + 信号信号量 + 条件判断
	1)两个全局变量 num 和 flag,解决问题a)关键在于生产者每生产一个数,num加一,唯有当flag == num时,生		  产者才可以生产下一个;消费者要做的就是将flag加一,以表示获取到一个数
	2)解决问题b)关键在于判断 flag 是否等于 num,当 flag == num 时阻塞当前线程并等待信号来临
	
难点:
	当消费者调用 pthread_cond_wait 表示该消费者进入阻塞并等待生产者发出信号唤醒,但该函数调用完之后并不意     味着下一个可运行线程就是生产者,有可能是其它消费者线程在运行,这意味着可能有多个消费者在等待信号来临。而生     产者调用一次 pthread_cond_signal 只能唤醒一个等待该信号的线程,所以其它阻塞的消费者仍在等待中,关键的     关键在于当最后一个数有多个消费者等待时,但只能有一个消费者获取由生产者提供的唤醒信号,所以生产者能安全退出     线程,获取到该信号量的消费者也能安全退出线程,但其它等待中的消费者仍在等待中,漫无目的的等待,永不终止...
	所以当最后一个数被等待中的其中一个消费者获取时,该消费者负责唤醒其他消费者线程并告诉它们没有数字可以给它们     了,此时大家都能安全退出线程。
**/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#define CUSTOMER_NUM 3

static pthread_mutex_t mutex;          
static pthread_mutexattr_t attr;
static pthread_cond_t cond1,cond2;
static int num = 1;
static int flag = 1;
//消费者个数
static pthread_t ps[CUSTOMER_NUM];

//消费者线程入口
static void func1(void* n)
{
	int i = *(int*)n;
	while(num != 10)
	{
		pthread_mutex_lock(&mutex);
		while(flag == num && flag != 10)
		{
			pthread_cond_wait(&cond1,&mutex);
            /** A --- 被唤醒,判断最后一个数是否已经没了 **/
			if(flag == 10)
				goto LOOP;
		}
		flag = num;
		printf("customer%d : num = %d\n",i,num);
		pthread_cond_signal(&cond2);
		if(flag == 10)
            /** B --- 最后一个数已经被获取了,负责唤醒所有等待该信号的消费者**/
			pthread_cond_broadcast(&cond1);
		pthread_mutex_unlock(&mutex);
	}
	printf("exit %d\n",i);
	pthread_exit(NULL);
	LOOP:
	printf("exit %d\n",i);
	pthread_mutex_unlock(&mutex);
	pthread_exit(NULL);
}

//生成者线程入口
static void func2()
{
	while(num != 10)
	{	
		pthread_mutex_lock(&mutex);
		while(flag != num)
		{
			pthread_cond_wait(&cond2,&mutex);
		}
		printf("---begin add!---\n");
		num++;
		pthread_cond_signal(&cond1);
		pthread_mutex_unlock(&mutex);
	}
	pthread_exit(NULL);
}

int main()
{
	int ret,i;
	int a[3] = {1,2,3};
	pthread_mutexattr_init(&attr);
	pthread_cond_init(&cond1,NULL);
	pthread_cond_init(&cond2,NULL);
	pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
	pthread_mutex_init(&mutex,&attr);
	pthread_t product;
	ret = pthread_create(&product,NULL,(void*)func2,NULL);
	if(ret != 0)
		exit(-1);
	for(i=0; i<CUSTOMER_NUM; i++)
	{
		ret = pthread_create(&ps[i],NULL,(void*)func1,&a[i]);
		if(ret != 0)
			exit(-1);
	}
	for(i=0; i<CUSTOMER_NUM; i++)
		pthread_join(ps[i],NULL);
	pthread_join(product,NULL);

	printf("----------\n");
	pthread_mutex_destroy(&mutex);
	pthread_cond_destroy(&cond1);
	pthread_cond_destroy(&cond2);
	return 0;
}

运行结果示例:
---begin add!---
customer1 : num = 2
---begin add!---
customer3 : num = 3
---begin add!---
customer1 : num = 4
---begin add!---
customer1 : num = 5
---begin add!---
customer1 : num = 6
---begin add!---
customer2 : num = 7
---begin add!---
customer3 : num = 8
---begin add!---
customer1 : num = 9
---begin add!---
customer2 : num = 10
exit 2
exit 1
exit 3
----------
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值