生产者/消费者问题是并发处理中遇到的最常见的问题,通常可描述如下:有一个或多个生产者生产某种类型的数据(记录,字符),并放置在缓冲区中;有一个消费者从缓冲区中取数据,每次取一项;系统保证避免对缓冲区的重复操作,也就是说,在任何时候只有一个主体(生产者或消费者)可以访问缓冲区。问题是要确保这样的情况,当缓冲区已满时,生产者不会继续向其中添加数据;当缓冲区为空时,消费者不会从中移走数据。
若缓冲区是队列的形式,该当如何:
#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; }结果如下: