Chinaunix首页 | 论坛 | 博客
  • 博客访问: 332077
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1293
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-07 11:17
个人简介

爱运动,爱看书,爱生活!

文章分类

全部博文(73)

文章存档

2014年(7)

2013年(66)

分类: C/C++

2013-09-08 20:14:28

简单,少量数据运行没问题,但直觉上还有错误,欢迎指出
直接上代码:

点击(此处)折叠或打开

  1. /*
  2.     生产者消费者模型模拟,循环队列、互斥量条件量维护缓冲池,多线程实现
  3. */
  4. #include <stdio.h>
  5. #include <pthread.h>
  6. #include <stdlib.h>

  7. #define BUFFERSIZE 100
  8. #define OVER (-1)

  9. /*建立缓冲区,里面包含一个队列,队列包含数据区,两个模拟指针(队头和队尾指针)
  10. 一个互斥量(mutex),两个条件量用来判断缓冲区的两种状态:full,empty
  11. */
  12. typedef struct prodBuffer{
  13.     int buffer[BUFFERSIZE];//数据区
  14.     int front;//头指针,拿数据
  15.     int rear;//尾指针,写数据
  16.     int count;//计数器
  17.     pthread_mutex_t mutex;//互斥锁
  18.     pthread_cond_t nofull;//缓冲区满的条件量
  19.     pthread_cond_t noempty;//缓冲区空的条件量
  20. }prodBuffer;

  21. //初始化缓冲区
  22. void init(prodBuffer *p)
  23. {
  24.     p->front=0;
  25.     p->rear=0;
  26.     p->count=0;
  27.     pthread_mutex_init(&p->mutex,0);
  28.     pthread_cond_init(&p->nofull,0);
  29.     pthread_cond_init(&p->noempty,0);
  30. }
  31. //撤销
  32. void destroy(prodBuffer *p)
  33. {
  34.     pthread_mutex_destroy(&p->mutex);
  35.     pthread_cond_destroy(&p->noempty);
  36.     pthread_cond_destroy(&p->nofull);
  37. }
  38. //往缓冲区写数据的函数
  39. void put(prodBuffer *p,int data)
  40. {
  41.     //加锁,不能同时对缓冲区操作
  42.      pthread_mutex_lock(&p->mutex);
  43.     //缓冲区是否以已满
  44.     if(p->count==BUFFERSIZE){
  45.         pthread_cond_wait(&p->nofull,&p->mutex);//缓冲区已满,阻塞等待
  46.     }
  47.     //写数据
  48.     p->buffer[p->rear]=data;
  49.     //移动队尾指针
  50.     p->rear=(p->rear+1)%BUFFERSIZE;
  51.     p->count++;
  52. //    if(p->count>=BUFFERSIZE)
  53. //    p->rear=(p->rear+1)%BUFFERSIZE;
  54.     //做出通知可以读数据,有数据了
  55.     pthread_cond_signal(&p->noempty);
  56.     //解锁
  57.     pthread_mutex_unlock(&p->mutex);

  58. }

  59. //往缓冲区读数据的函数
  60. int get(prodBuffer *p)
  61. {
  62.     int data;
  63.     pthread_mutex_lock(&p->mutex);
  64.     //判断是否缓冲区空
  65.     if(p->count==0){
  66.         //等待有数据
  67.         pthread_cond_wait(&p->noempty,&p->mutex);
  68.     }
  69.     //读数据
  70.     data=p->buffer[p->front];
  71.     //移动指针
  72.     p->front=(p->front+1)%BUFFERSIZE;
  73.     p->count--;
  74. //    if(p->count>=BUFFERSIZE)
  75. //    p->front=0;
  76.     pthread_cond_signal(&p->nofull);
  77.     pthread_mutex_unlock(&p->mutex);
  78.     return data;
  79. }

  80. prodBuffer buf;

  81. //生成者线程模拟
  82. void *producter(void *d)
  83. {
  84.     int i;
  85.     for(i=0;i<1000;i++){
  86.         put(&buf,i);
  87.          printf("%d ",i);
  88.     }
  89.         printf("\n");
  90. //    put(&buf,OVER);
  91.     return NULL;
  92. }


  93. //消费者线程模拟
  94. void *consumer(void *d)
  95. {
  96.     while(1){
  97.         int d=get(&buf);
  98.         printf("%d ",d);
  99.     }    
  100.     printf("\n");
  101.     return NULL;
  102. }
  103. int main()
  104. {
  105.     pthread_t th_a,th_b;
  106.     init(&buf);    
  107.     pthread_create(&th_a,NULL,producter,0);
  108.     pthread_create(&th_b,NULL,consumer,0);
  109.     pthread_join(th_a,(void**)0);
  110.     pthread_join(th_b,(void**)0);
  111.     destroy(&buf);
  112.     return 0;
  113. }

阅读(3401) | 评论(0) | 转发(0) |
0

上一篇:字符串之子串

下一篇:C++ Vector 使用总结

给主人留下些什么吧!~~