-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
48 lines (37 loc) · 906 Bytes
/
main.cpp
File metadata and controls
48 lines (37 loc) · 906 Bytes
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
#include <pthread.h>
#include <semaphore.h>
#include <cstdlib>
#include <iostream>
#include "producer.cpp"
#include "consumer.cpp"
#define maxBuffer 2
#define bufferSize 2
sem_t empty;
sem_t full;
int in = 0;
int out = 0;
int table[bufferSize];
pthread_mutex_t mutex;
int main(){
pthread_t pro[2], con[2];
pthread_mutex_init(&mutex, NULL);
sem_init(&empty, 0, bufferSize);
sem_init(&full, 0, 0);
int a[5] = { 1, 2 };
for (int i = 0; i < 2; i++) {
pthread_create(&pro[i], NULL, producer, (void*)&a[i]);
}
for (int i = 0; i < 2; i++) {
pthread_create(&con[i], NULL, consumer, (void*)&a[i]);
}
for (int i = 0; i < 2; i++) {
pthread_join(pro[i], NULL);
}
for (int i = 0; i < 2; i++) {
pthread_join(con[i], NULL);
}
pthread_mutex_destroy(&mutex);
sem_destroy(&empty);
sem_destroy(&full);
return 0;
}