Linux下c语言多线程实现生产者-消费者编程

Linux下c语言多线程实现生产者-消费者问题

利用循环队列实现资源池,生产者和消费者互斥的进行访问,关键是要定义好资源池的属性,定义如下:

struct prodcons
{
	int buffer[BUFFER_SIZE];
	pthread_mutex_t lock;
	int readpos, writepos;
	pthread_cond_t notempty;
	pthread_cond_t notfull;
};

 5个属性表示的含义分别是:

资源池空间、互斥锁、生产者和消费者position、资源池空和满的条件变量

 

实现的代码如下:

/*************************************************************************
  > File Name: pro_cus.c
  > Author: xiao5
  > Mail: xiao5_zju@163.com 
  > Created Time: Mon 13 Jun 2016 10:31:34 AM CST
 ************************************************************************/

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

#define BUFFER_SIZE 10

struct prodcons
{
	int buffer[BUFFER_SIZE];
	pthread_mutex_t lock;
	int readpos, writepos;
	pthread_cond_t notempty;
	pthread_cond_t notfull;
};

void init(struct prodcons *b)
{
	printf("Starting init the struct Prodcons!\n");
	sleep(1);
	pthread_mutex_init(&b->lock, NULL);
	pthread_cond_init(&b->notempty, NULL);
	pthread_cond_init(&b->notfull, NULL);
	b->readpos = 0;
	b->writepos = 0;
}

void put(struct prodcons *b, int data)
{
	pthread_mutex_lock(&b->lock);

	while(((b->writepos+1) % BUFFER_SIZE == b->readpos))
	{
		printf("队列已满,等待消费者消费产品......\n");
		pthread_cond_wait(&b->notfull, &b->lock);
	}

	b->buffer[b->writepos] = data;
	b->writepos++;
	if((b->writepos) == BUFFER_SIZE) b->writepos = 0;
	pthread_cond_signal(&b->notempty);
	pthread_mutex_unlock(&b->lock);
}

int get(struct prodcons *b)
{
	int data;
	pthread_mutex_lock(&b->lock);
	while(b->writepos == b->readpos)
	{
		printf("队列中没有产品,等待生产者生产......\n");
		pthread_cond_wait(&b->notempty, &b->lock);
	}

	data = b->buffer[b->readpos];
	b->readpos++;
	if(b->readpos >= BUFFER_SIZE) b->readpos = 0;
	pthread_cond_signal(&b->notfull);
	pthread_mutex_unlock(&b->lock);
	return data;
}

#define OVER -1

struct prodcons buffer;

void *producer(void *data)
{
	int n;
	for(n=0; n<30; n++)
	{
		printf("Start put %d\t", n);
		put(&buffer, n);
		printf("Put --> %d finidhed!\n", n);
	}
	put(&buffer, OVER);
	printf("Producer finish the job!\n");
	return NULL;
}

void *consumer(void *data)
{
	int d = -1;
	while(1)
	{
		printf("Start get %d\t", ++d);
		d = get(&buffer);
		printf("%d --> get finished!\n", d);
		if(d == OVER) break;
	}
	printf("Consumer finish the job!\n");
	return NULL;
}

int main()
{
	pthread_t tha, thb;
	void * retval;

	init(&buffer);
	pthread_create(&tha, NULL, producer, 0);
	pthread_create(&thb, NULL, consumer, 0);

	pthread_join(tha, &retval);
	pthread_join(thb, &retval);

	return 0;
}

 

实现的结果如下:





  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值