点击(此处)折叠或打开
- /************************************************
- *
- * The classic producer-consumer example.
- * Illustrates mutexes and conditions.
- * by Zou jian guo <ah_zou@tom.com>
- * 2003-12-22
- *
- *************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "pthread.h"
- #define BUFFER_SIZE 16
- /* Circular buffer of integers. */
- struct prodcons {
- int buffer[BUFFER_SIZE]; /* the actual data */
- pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
- int readpos, writepos; /* positions for reading and writing */
- pthread_cond_t notempty; /* signaled when buffer is not empty */
- pthread_cond_t notfull; /* signaled when buffer is not full */
- };
- /*--------------------------------------------------------*/
- /* Initialize a buffer */
- void init(struct prodcons * b)
- {
- pthread_mutex_init(&b->lock, NULL);/*初始化互斥锁*/
- pthread_cond_init(&b->notempty, NULL);/*初始化条件变量notempty*/
- pthread_cond_init(&b->notfull, NULL);/*初始化条件变量notfull*/
- b->readpos = 0;/*初始化读位置*/
- b->writepos = 0;/*初始化写位置*/
- }
- /*--------------------------------------------------------*/
- /* Store an integer in the buffer */
- void put(struct prodcons * b, int data)/*生产*/
- {
- pthread_mutex_lock(&b->lock); /*加锁*/
- /* Wait until buffer is not full */ /*有一个空位就相当于满*/
- while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { /*判断读写位置是否相等;+1防止两个进程都阻塞*/
- printf("wait for not full\n");
- pthread_cond_wait(&b->notfull, &b->lock);/*解lock互斥锁并阻塞,等待消费者notfull来解除阻塞,
- 被唤醒时再加lock锁*/
- }
- /* Write the data and advance write pointer */
- b->buffer[b->writepos] = data; /*写入数据(产品放入共享区域)*/
- b->writepos++; /*写位置+1 */
- if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
- /* Signal that the buffer is now not empty */
- pthread_cond_signal(&b->notempty); /*释放阻塞在notempty条件变量上的(消费者)线程*/
- pthread_mutex_unlock(&b->lock); /*解锁*/
- }
- /*--------------------------------------------------------*/
- /* Read and remove an integer from the buffer */
- int get(struct prodcons * b)/*消费*/
- {
- int data;
- pthread_mutex_lock(&b->lock);/*加锁*/
- /* Wait until buffer is not empty */
- while (b->writepos == b->readpos) { /*判断读写位置是否相等*/
- printf("wait for not empty\n");
- pthread_cond_wait(&b->notempty, &b->lock);/*解lock互斥锁并阻塞,等待消费者notempty来解除阻塞
- 被唤醒时,再加lock锁*/
- }
- /* Read the data and advance read pointer */
- data = b->buffer[b->readpos]; /*读数据(消费产品)*/
- b->readpos++; /*读位置+1*/
- if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
- /* Signal that the buffer is now not full */
- pthread_cond_signal(&b->notfull); /*释放阻塞在notfull条件变量上的(生产者)线程*/
- pthread_mutex_unlock(&b->lock); /*解锁*/
- return data;
- }
- /*--------------------------------------------------------*/
- #define OVER (-1)
- struct prodcons buffer;
- /*--------------------------------------------------------*/
- void * producer(void * data)/*生产者函数*/
- {
- int n;
- for (n = 0; n < 1000; n++) {/*总产量为1000,生产1000个产品,停止生产*/
- printf(" put-->%d\n", n);
- put(&buffer, n);/*生产产品,放入共享区域*/
- }
- put(&buffer, OVER);/*生产结束标志*/
- printf("producer stopped!\n");
- return NULL;
- }
- /*--------------------------------------------------------*/
- void * consumer(void * data)/*消费者函数*/
- {
- int d;
- while (1) {
- d = get(&buffer);/*消费产品*/
- if (d == OVER ) break;
- printf(" %d-->get\n", d);
- }
- printf("consumer stopped!\n");
- return NULL;
- }
- /*--------------------------------------------------------*/
- int main(void)
- {
- pthread_t th_a, th_b;
- void * retval;
- init(&buffer);/*初始化共享区域*/
- pthread_create(&th_a, NULL, producer, 0);/*创建生产者线程*/
- pthread_create(&th_b, NULL, consumer, 0);/*创建消费者线程*/
- /* Wait until producer and consumer finish. */
- pthread_join(th_a, &retval);/*主线程等该生产者线程*/
- pthread_join(th_b, &retval);/*主线程等该消费者线程*/
- return 0;
- }
相关热门文章
给主人留下些什么吧!~~
评论热议