pthread-生产消费.c -----注释

来源:互联网 发布:linux竖线怎么打 编辑:程序博客网 时间:2024/05/17 22:03

点击(此处)折叠或打开

  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. }

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(215) | 评论(0) | 转发(0) |
0

上一篇:+-小数四舍五入c代码---

下一篇:Linux stat函数讲解

相关热门文章
  • test123
  • 编写安全代码——小心有符号数...
  • 彻底搞定C语言指针详解-完整版...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • linux dhcp peizhi roc
  • 关于Unix文件的软链接
  • 求教这个命令什么意思,我是新...
  • sed -e "/grep/d" 是什么意思...
  • 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
原创粉丝点击