Chinaunix首页 | 论坛 | 博客
  • 博客访问: 430282
  • 博文数量: 168
  • 博客积分: 320
  • 博客等级: 二等列兵
  • 技术积分: 955
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-21 08:40
个人简介

知足却不乏追求

文章分类

全部博文(168)

文章存档

2017年(1)

2016年(6)

2015年(36)

2014年(5)

2013年(6)

2012年(114)

分类: C/C++

2012-10-24 16:37:18


点击(此处)折叠或打开

  1. /************************************************

  2.  *

  3.  *    The classic producer-consumer example.

  4.  *     Illustrates mutexes and conditions.

  5.  * by Zou jian guo <ah_zou@tom.com>

  6.  * 2003-12-22

  7.  *

  8. *************************************************/



  9. #include <stdio.h>

  10. #include <stdlib.h>

  11. #include <time.h>

  12. #include "pthread.h"



  13. #define BUFFER_SIZE 16



  14. /* Circular buffer of integers. */

  15. struct prodcons {

  16.   int buffer[BUFFER_SIZE]; /* the actual data */

  17.   pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */

  18.   int readpos, writepos; /* positions for reading and writing */

  19.   pthread_cond_t notempty; /* signaled when buffer is not empty */

  20.   pthread_cond_t notfull; /* signaled when buffer is not full */

  21. };



  22. /*--------------------------------------------------------*/

  23. /* Initialize a buffer */

  24. void init(struct prodcons * b)

  25. {

  26.   pthread_mutex_init(&b->lock, NULL);/*初始化互斥锁*/

  27.   pthread_cond_init(&b->notempty, NULL);/*初始化条件变量notempty*/

  28.   pthread_cond_init(&b->notfull, NULL);/*初始化条件变量notfull*/

  29.   b->readpos = 0;/*初始化读位置*/

  30.   b->writepos = 0;/*初始化写位置*/

  31. }

  32. /*--------------------------------------------------------*/

  33. /* Store an integer in the buffer */

  34. void put(struct prodcons * b, int data)/*生产*/

  35. {

  36.     pthread_mutex_lock(&b->lock);    /*加锁*/



  37.       /* Wait until buffer is not full */                                            /*有一个空位就相当于满*/

  38.      while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) {    /*判断读写位置是否相等;+1防止两个进程都阻塞*/

  39.         printf("wait for not full\n");

  40.         pthread_cond_wait(&b->notfull, &b->lock);/*解lock互斥锁并阻塞,等待消费者notfull来解除阻塞,
  41.                                                     被唤醒时再加lock锁*/

  42.       }

  43.   /* Write the data and advance write pointer */

  44.       b->buffer[b->writepos] = data;    /*写入数据(产品放入共享区域)*/

  45.       b->writepos++;                    /*写位置+1 */

  46.       if (b->writepos >= BUFFER_SIZE) b->writepos = 0;

  47.   /* Signal that the buffer is now not empty */

  48.       pthread_cond_signal(&b->notempty);    /*释放阻塞在notempty条件变量上的(消费者)线程*/



  49.     pthread_mutex_unlock(&b->lock);        /*解锁*/

  50. }

  51. /*--------------------------------------------------------*/

  52. /* Read and remove an integer from the buffer */

  53. int get(struct prodcons * b)/*消费*/

  54. {

  55.       int data;

  56.     pthread_mutex_lock(&b->lock);/*加锁*/



  57.      /* Wait until buffer is not empty */

  58.       while (b->writepos == b->readpos) {        /*判断读写位置是否相等*/

  59.         printf("wait for not empty\n");

  60.         pthread_cond_wait(&b->notempty, &b->lock);/*解lock互斥锁并阻塞,等待消费者notempty来解除阻塞
  61.                                                         被唤醒时,再加lock锁*/

  62.       }

  63.       /* Read the data and advance read pointer */

  64.       data = b->buffer[b->readpos];    /*读数据(消费产品)*/

  65.       b->readpos++;                    /*读位置+1*/

  66.       if (b->readpos >= BUFFER_SIZE) b->readpos = 0;

  67.       /* Signal that the buffer is now not full */

  68.       pthread_cond_signal(&b->notfull);    /*释放阻塞在notfull条件变量上的(生产者)线程*/



  69.       pthread_mutex_unlock(&b->lock);        /*解锁*/

  70.       return data;

  71. }

  72. /*--------------------------------------------------------*/

  73. #define OVER (-1)

  74. struct prodcons buffer;

  75. /*--------------------------------------------------------*/

  76. void * producer(void * data)/*生产者函数*/

  77. {

  78.       int n;

  79.       for (n = 0; n < 1000; n++) {/*总产量为1000,生产1000个产品,停止生产*/

  80.         printf(" put-->%d\n", n);

  81.         put(&buffer, n);/*生产产品,放入共享区域*/

  82.     }

  83.   put(&buffer, OVER);/*生产结束标志*/

  84.   printf("producer stopped!\n");

  85.   return NULL;

  86. }

  87. /*--------------------------------------------------------*/

  88. void * consumer(void * data)/*消费者函数*/

  89. {

  90.   int d;

  91.   while (1) {

  92.     d = get(&buffer);/*消费产品*/

  93.     if (d == OVER ) break;

  94.     printf(" %d-->get\n", d);

  95.   }

  96.   printf("consumer stopped!\n");

  97.   return NULL;

  98. }

  99. /*--------------------------------------------------------*/

  100. int main(void)

  101. {

  102.       pthread_t th_a, th_b;

  103.       void * retval;



  104.       init(&buffer);/*初始化共享区域*/

  105.      pthread_create(&th_a, NULL, producer, 0);/*创建生产者线程*/

  106.       pthread_create(&th_b, NULL, consumer, 0);/*创建消费者线程*/

  107.   /* Wait until producer and consumer finish. */

  108.       pthread_join(th_a, &retval);/*主线程等该生产者线程*/

  109.       pthread_join(th_b, &retval);/*主线程等该消费者线程*/



  110.       return 0;

  111. }

阅读(677) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~