linux线程-生产者/消费者

  1. #include <stdio.h>  
  2. #include <pthread.h>  
  3. #define BUFFER_SIZE 16 // 缓冲区数量  
  4. struct prodcons  
  5. {  
  6.     // 缓冲区相关数据结构  
  7.     int buffer[BUFFER_SIZE]; /* 实际数据存放的数组*/  
  8.     pthread_mutex_t lock; /* 互斥体lock 用于对缓冲区的互斥操作 */  
  9.     int readpos, writepos; /* 读写指针*/  
  10.     pthread_cond_t notempty; /* 缓冲区非空的条件变量 */  
  11.     pthread_cond_t notfull; /* 缓冲区未满的条件变量 */  
  12. };  
  13. /* 初始化缓冲区结构 */  
  14. void init(struct prodcons *b)  
  15. {  
  16.     pthread_mutex_init(&b->lock, NULL);  
  17.     pthread_cond_init(&b->notempty, NULL);  
  18.     pthread_cond_init(&b->notfull, NULL);  
  19.     b->readpos = 0;  
  20.     b->writepos = 0;  
  21. }  
  22. /* 将产品放入缓冲区,这里是存入一个整数*/  
  23. void put(struct prodcons *b, int data)  
  24. {  
  25.     pthread_mutex_lock(&b->lock);  
  26.     /* 等待缓冲区未满*/  
  27.     if ((b->writepos + 1) % BUFFER_SIZE == b->readpos)  
  28.     {  
  29.         pthread_cond_wait(&b->notfull, &b->lock);  
  30.     }  
  31.     /* 写数据,并移动指针 */  
  32.     b->buffer[b->writepos] = data;  
  33.     b->writepos++;  
  34.     if (b->writepos >= BUFFER_SIZE)  
  35.         b->writepos = 0;  
  36.     /* 设置缓冲区非空的条件变量*/  
  37.     pthread_cond_signal(&b->notempty);  
  38.     pthread_mutex_unlock(&b->lock);  
  39. }   
  40. /* 从缓冲区中取出整数*/  
  41. int get(struct prodcons *b)  
  42. {  
  43.     int data;  
  44.     pthread_mutex_lock(&b->lock);  
  45.     /* 等待缓冲区非空*/  
  46.     if (b->writepos == b->readpos)  
  47.     {  
  48.         pthread_cond_wait(&b->notempty, &b->lock);  
  49.     }  
  50.     /* 读数据,移动读指针*/  
  51.     data = b->buffer[b->readpos];  
  52.     b->readpos++;  
  53.     if (b->readpos >= BUFFER_SIZE)  
  54.         b->readpos = 0;  
  55.     /* 设置缓冲区未满的条件变量*/  
  56.     pthread_cond_signal(&b->notfull);  
  57.     pthread_mutex_unlock(&b->lock);  
  58.     return data;  
  59. }  
  60.   
  61.   
  62. /* 测试:生产者线程将1 到10000 的整数送入缓冲区,消费者线 
  63.    程从缓冲区中获取整数,两者都打印信息*/  
  64. #define OVER ( - 1)  
  65. struct prodcons buffer;  
  66. void *producer(void *data)  
  67. {  
  68.     int n;  
  69.     for (n = 0; n < 10000; n++)  
  70.     {  
  71.         printf("%d --->\n", n);  
  72.         put(&buffer, n);  
  73.     } put(&buffer, OVER);  
  74.     return NULL;  
  75. }  
  76.   
  77.   
  78. void *consumer(void *data)  
  79. {  
  80.     int d;  
  81.     while (1)  
  82.     {  
  83.         d = get(&buffer);  
  84.         if (d == OVER)  
  85.             break;  
  86.         printf("--->%d \n", d);  
  87.     }  
  88.     return NULL;  
  89. }  
  90.   
  91.   
  92. int main(void)  
  93. {  
  94.     pthread_t th_a, th_b;  
  95.     void *retval;  
  96.     init(&buffer);  
  97.     /* 创建生产者和消费者线程*/  
  98.     pthread_create(&th_a, NULL, producer, 0);  
  99.     pthread_create(&th_b, NULL, consumer, 0);  
  100.     /* 等待两个线程结束*/  
  101.     pthread_join(th_a, &retval);  
  102.     pthread_join(th_b, &retval);  
  103.     return 0;  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sunxiaopengsun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值