1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include "pthread.h"
5 #define BUFFER_SIZE 1600
6 #define OVER (110000)
7 /* 设置一个整数的圆形缓冲区 */
8 struct prodcons {
9 int buffer[BUFFER_SIZE]; /* 缓冲区数组 */
10 pthread_mutex_t lock; /* 互斥锁 */
11 int readpos, writepos; /* 读写的位置*/
12 int ji, ou, zheng, fu, zero; //奇数、偶数、正数、负数,零的个数
13 pthread_cond_t readable; /* 缓冲区非空信号 */
14 pthread_cond_t writeable; /*缓冲区非满信号 */
15 };
16 /*--------------------------------------------------------*/
17 /*初始化缓冲区*/
18 void init(struct prodcons * b)
19 {
20 pthread_mutex_init(&b->lock, NULL);
21 pthread_cond_init(&b->readable, NULL);
22 pthread_cond_init(&b->writeable, NULL);
23 b->readpos = 0;
24 b->writepos = 0;
25 b->ji=0;
26 b->ou=0;
27 b->zheng=0;
28 b->fu=0;
29 b->zero=0;
30 }
31 /*--------------------------------------------------------*/
32 /* 向缓冲区中写入一个整数*/
33 void put(struct prodcons * b, int data)
34 {
35 pthread_mutex_lock(&b->lock);
36 /*等待缓冲区非满*/
37 //写的话,我就不等了
38 // while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
39 // while (b->writepos == b->readpos) {
40 // printf("wait for not write able\n");
41 // pthread_cond_wait(&b->writeable, &b->lock);
42 // }
43 /*写数据并且指针前移*/
44 b->buffer[b->writepos] = data;
45 b->writepos++;
46 // if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
47 /*设置缓冲区非空信号*/
48 if (b->writepos > b->readpos)
49 pthread_cond_signal(&b->readable);
50 pthread_mutex_unlock(&b->lock);
51 }
52 /*--------------------------------------------------------*/
53 /*从缓冲区中读出一个整数 */
54 int get(struct prodcons * b)
55 {
56 int data;
57 pthread_mutex_lock(&b->lock);
58 /* 等待缓冲区非空*/
59 while (b->writepos <= b->readpos) {
60 printf("wait for new data\n");
61 pthread_cond_wait(&b->readable, &b->lock);
62 }
63 /* 读数据并且指针前移 */
64 data = b->buffer[b->readpos];
65 b->readpos++;
66 //统计
67 if(data!=OVER){
68 if(data%2==0) b->ou++;
69 else b->ji++;
70 if(data==0) b->zero++;
71 else if(data>0) b->zheng++;
72 else b->fu++;
73 }
74 // if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
75 /* 设置缓冲区非满信号*/
76 // pthread_cond_signal(&b->writeable);
77 pthread_mutex_unlock(&b->lock);
78 return data;
79 }
80 /*--------------------------------------------------------*/
81 struct prodcons buffer;
82 /*--------------------------------------------------------*/
83 void * producer(void * data)
84 {
85 int n;
86 int m;
87 srand(time(0));
88 for (n = 0; n < 1000; n++) {
89 // srand(time(0));
90 // m=(rand()*1000)%1000;
91 m=(rand())%1000-500;
92 printf(" wirte-->%d\n", m);
93 put(&buffer, m);
94 }
95 put(&buffer, OVER);
96 printf("producer stopped!\n");
97 return NULL;
98 }
99 /*--------------------------------------------------------*/
100 void * consumer(void * data)
101 {
102 int d;
103 while (1) {
104 d = get(&buffer);
105 if (d == OVER ) break;
106 printf(" %d-->read\n", d);
107 }
108 printf("consumer stopped!\n");
109 return NULL;
110 }
111 /*--------------------------------------------------------*/
112 int main(void)
113 {
114 pthread_t th_a, th_b;
115 void * retval;
116 init(&buffer);
117 pthread_create(&th_a, NULL, producer, 0);
118 pthread_create(&th_b, NULL, consumer, 0);
119 /* 等待生产者和消费者结束 */
120 pthread_join(th_a, &retval);
121 pthread_join(th_b, &retval);
122 printf("奇数个数:%d\n",buffer.ji);
123 printf("偶数个数:%d\n",buffer.ou);
124 printf("正数个数:%d\n",buffer.zheng);
125 printf("负数个数:%d\n",buffer.fu);
126 printf("零的个数:%d\n",buffer.zero);
127 return 0;
128 }