【新手入门向 浅显易懂】分别用栈和队列实现生产者与消费者模型

栈实现模型:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
*// 仓库容量*
#define STORE_SIZE (20)
*// 仓库*
char store[STORE_SIZE];
*// 数量*
size_t store_cnt = 0;

pthread_mutex_t lock;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;

void show_store(const char* who,const char* act,char data)
{
	printf("[");
	for(int i=0; i<store_cnt; i++)
	{
		printf("%c",store[i]);
	}
	printf("]%s%c : %s\n",act,data,who);
}

void* prun(void* arg)
{
	for( ; ; )
	{
		*// 加锁*
		pthread_mutex_lock(&lock);
		while(store_cnt >= STORE_SIZE)
		{
			*// 此时仓库已满,生产者睡入仓库满的条件变量*
			pthread_cond_wait(&cond_full,&lock);
		}

		*// 生产的数据*
		char data = rand() % 26 + 'A';
		show_store("生产者","<-",data);
		store[store_cnt++] = data;
		usleep(rand()%30000);

		*// 解锁*
		pthread_mutex_unlock(&lock);
		
		// 叫醒所有睡入仓库空条件变量的线程
		// pthread_cond_signal(&cond_empty);
		pthread_cond_broadcast(&cond_empty);
	}
}

void* crun(void* arg)
{
	for( ; ; )
	{
		*// 加锁*
		pthread_mutex_lock(&lock);
		while(0 >= store_cnt)
		{
			// 此时仓库为空,生产者睡入仓库空的条件变量
			pthread_cond_wait(&cond_empty,&lock);	
		}
		*// 消费的数据*
		char data = store[--store_cnt];
		show_store("消费者","->",data);
		usleep(rand()%30000);

		*// 解锁*
		pthread_mutex_unlock(&lock);

		*// 叫醒所有睡入仓库满条件变量的线程*
		// pthread_cond_signal(&cond_full);
		pthread_cond_broadcast(&cond_full);
	}
}

int main()
{
	srand(time(NULL));
	*// 初始化互斥量与条件变量*
	pthread_mutex_init(&lock,NULL);
	pthread_cond_init(&cond_empty,NULL);
	pthread_cond_init(&cond_full,NULL);

	pthread_t tid[10];
	*//创建生产者线程*
	for(int i=0; i<5; i++)
	{
		if(i%2)
			pthread_create(&tid[i],NULL,prun,NULL);
		else
			pthread_create(&tid[i],NULL,crun,NULL);
	}

	*// 等待所有线程结束*
	for(int i=0; i<10; i++)
	{
		pthread_join(tid[i],NULL);
	}
}

队列模型实现:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
*// 仓库容量*
#define STORE_SIZE (20)
*// 仓库*
char store[STORE_SIZE];
*// 队头和队尾*
int front = 0 , rear = 0;

pthread_mutex_t flock;
pthread_mutex_t rlock;
pthread_cond_t cond_empty;
pthread_cond_t cond_full;

void show_store(const char* who,const char* act,char data)
{
	printf("[");
	for(int i=front; i!=rear; i=(i+1)%STORE_SIZE)
	{
		printf("%c",store[i]);
	}
	printf("]%s%c : %s\n",act,data,who);
}

void* prun(void* arg)
{
	for(;;)
	{
		*// 加锁*
		pthread_mutex_lock(&flock);
		while((rear+1)%STORE_SIZE == front)
		{
			// 此时仓库已满,生产者睡入仓库满的条件变量
			pthread_cond_wait(&cond_full,&flock);
		}

		*// 生产的数据*
		char data = rand() % 26 + 'A';
		show_store("生产者","<-",data);
		store[rear++] = data;
		rear %= STORE_SIZE; 
		usleep(rand()%500000);
		
		*// 叫醒所有睡入仓库空条件变量的线程*
		pthread_cond_broadcast(&cond_empty);

		*// 解锁*
		pthread_mutex_unlock(&flock);
	}
}

void* crun(void* arg)
{
	for(;;)
	{
		*// 加锁*
		pthread_mutex_lock(&rlock);
		while(front == rear)
		{
			*// 此时仓库为空,生产者睡入仓库空的条件变量*
			pthread_cond_wait(&cond_empty,&rlock);	
		}

		*// 消费的数据*
		char data = store[front++];
		front %= STORE_SIZE;
		show_store("消费者","->",data);
		usleep(rand()%500000);

		*// 叫醒所有睡入仓库满条件变量的线程*
		pthread_cond_broadcast(&cond_full);

		*// 解锁*
		pthread_mutex_unlock(&rlock);
	}
}

int main()
{
	srand(time(NULL));
	*// 初始化互斥量与条件变量*
	pthread_mutex_init(&flock,NULL);
	pthread_mutex_init(&rlock,NULL);
	pthread_cond_init(&cond_empty,NULL);
	pthread_cond_init(&cond_full,NULL);

	pthread_t tid[10];
	*//创建生产者线程*
	for(int i=0; i<10; i++)
	{
		if(i%2)
			pthread_create(&tid[i],NULL,prun,NULL);
		else
			pthread_create(&tid[i],NULL,crun,NULL);
	}

	*// 等待所有线程结束*
	for(int i=0; i<10; i++)
	{
		pthread_join(tid[i],NULL);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值