Chinaunix首页 | 论坛 | 博客
  • 博客访问: 406722
  • 博文数量: 139
  • 博客积分: 106
  • 博客等级: 民兵
  • 技术积分: 613
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-24 16:03
文章分类

全部博文(139)

文章存档

2016年(1)

2014年(16)

2013年(23)

2012年(98)

2011年(1)

分类:

2012-10-08 10:36:00

程序要求:

  在程序当中,创建两个线程,其中一个为生产者,一个为消费者。同时创建一把互斥锁保证两者不能够同时访问结构体当中buffer,可以把它比作是一个“仓库”。必须要等生产完成之后才可以去采用读取。添加条件变量是与锁综合起来的条件判定。

  具体程序如下:


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "pthread.h"

  5. #define BUFFER_SIZE 2

  6. /* Circular buffer of integers. */
  7. struct prodcons
  8. {
  9.   int buffer[BUFFER_SIZE]; /* the actual data */
  10.   pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
  11.   int readpos, writepos; /* positions for reading and writing */
  12.   pthread_cond_t notempty; /* signaled when buffer is not empty */
  13.   pthread_cond_t notfull; /* signaled when buffer is not full */
  14. };

  15. /* Initialize a buffer */
  16. void init(struct prodcons *prod)
  17. {
  18.   pthread_mutex_init(&prod->lock,NULL);
  19.   pthread_cond_init(&prod->notempty,NULL);
  20.   pthread_cond_init(&prod->notfull,NULL);
  21.   prod->readpos = 0;
  22.   prod->writepos = 0;
  23. }
  24. /* Store an integer in the buffer */
  25. void put(struct prodcons * prod, int data)
  26. {
  27.         pthread_mutex_lock(&prod->lock);
  28.         /* Wait until buffer is not full */
  29.         while ((prod->writepos + 1) % BUFFER_SIZE == prod->readpos)
  30.         {
  31.             printf("producer wait for not full\n");
  32.             pthread_cond_wait(&prod->notfull, &prod->lock);
  33.         }
  34.           /* Write the data and advance write pointer */
  35.         prod->buffer[prod->writepos] = data;
  36.         prod->writepos++;
  37.         if (prod->writepos >= BUFFER_SIZE)
  38.             prod->writepos = 0;
  39.           /*Signal that the buffer is now not empty */
  40.         pthread_cond_signal(&prod->notempty);
  41.         pthread_mutex_unlock(&prod->lock);
  42. }
  43. /* Read and remove an integer from the buffer */
  44. int get(struct prodcons *prod)
  45. {
  46.         int data;
  47.         pthread_mutex_lock(&prod->lock);
  48.         /* Wait until buffer is not empty */
  49.         while (prod->writepos == prod->readpos)
  50.         {
  51.             printf("consumer wait for not empty\n");
  52.             pthread_cond_wait(&prod->notempty, &prod->lock);
  53.         }
  54.         /* Read the data and advance read pointer */
  55.         data = prod->buffer[prod->readpos];
  56.         prod->readpos++;
  57.         if (prod->readpos >= BUFFER_SIZE)
  58.             prod->readpos = 0;
  59.         /* Signal that the buffer is now not full */
  60.         pthread_cond_signal(&prod->notfull);
  61.         pthread_mutex_unlock(&prod->lock);
  62.         return data;
  63. }

  64. #define OVER (-1)
  65. struct prodcons buffer;
  66. /*--------------------------------------------------------*/
  67. void * producer(void * data)
  68. {
  69.     int n;
  70.     for (n = 0; n < 5; n++)
  71.     {
  72.         printf("producer sleep 1 second......\n");
  73.         sleep(1);
  74.         printf("put the %d product\n", n);
  75.         put(&buffer, n);
  76.     }
  77.     for(n=5; n<10; n++)
  78.     {
  79.         printf("producer sleep 3 second......\n");
  80.         sleep(3);
  81.         printf("put the %d product\n",n);
  82.         put(&buffer,n);
  83.     }
  84.     put(&buffer, OVER);
  85.     printf("producer stopped!\n");
  86.     return NULL;
  87. }
  88. /*--------------------------------------------------------*/
  89. void * consumer(void * data)
  90. {
  91.     int d=0;
  92.     while (1)
  93.     {
  94.         printf("consumer sleep 2 second......\n");
  95.         sleep(2);
  96.         d=get(&buffer);
  97.         printf("get the %d product\n", d);
  98. // d = get(&buffer);
  99.         if (d == OVER ) break;
  100.     }
  101.     printf("consumer stopped!\n");
  102.     return NULL;
  103. }
  104. /*--------------------------------------------------------*/
  105. int main(int argc,char *argv[])
  106. {
  107.     pthread_t th_a, th_b;
  108.     void * retval;
  109.     init(&buffer);
  110.     pthread_create(&th_a, NULL, producer, 0);
  111.     pthread_create(&th_b, NULL, consumer, 0);
  112.     /* Wait until producer and consumer finish. */
  113.     pthread_join(th_a, &retval);
  114.     pthread_join(th_b, &retval);
  115.     return 0;
  116. }



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