-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumerproducer.cpp
74 lines (62 loc) · 1.37 KB
/
consumerproducer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <pthread.h>
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t full = PTHREAD_COND_INITIALIZER;
pthread_cond_t empty = PTHREAD_COND_INITIALIZER;
const int mymax = 10;
//char buf[mymax];
int num;
int ccid = 0;
int ppid = 0;
void consume()
{
--num;
}
void *consumer(void *p)
{
int id = ++ccid;
while (1)
{
sleep(3);
pthread_mutex_lock(&mutex);
while (num <= 0)
pthread_cond_wait(&empty, &mutex);
consume();
cout<< "conumer thread "<<id<<" consue, left num="<<num<<endl;
pthread_cond_signal(&full);
pthread_mutex_unlock(&mutex);
}
}
void produce()
{
++num;
}
void *producer(void *p)
{
int id = ++ppid;
while(1)
{
sleep(1);
pthread_mutex_lock(&mutex);
while (num >= mymax)
pthread_cond_wait(&full, &mutex);
produce();
cout<< "producer thread "<<id<<" produce, left num="<<num<<endl;
pthread_cond_signal(&empty);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
pthread_t pid1,pid2,cid1,cid2;
pthread_create(&pid1, NULL, producer, NULL);
pthread_create(&pid2, NULL, producer, NULL);
pthread_create(&cid1, NULL, consumer, NULL);
pthread_create(&cid2, NULL, consumer, NULL);
pthread_join(pid1, NULL);
pthread_join(pid2, NULL);
pthread_join(cid1, NULL);
pthread_join(cid2, NULL);
return 0;
}