-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclicbuffer.cpp
136 lines (115 loc) · 2.59 KB
/
cyclicbuffer.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <iostream>
#include <pthread.h>
#include <atomic>
#include <vector>
using namespace std;
typedef struct
{
atomic_bool locked;
} my_mutex_t;
void my_mutex_init(my_mutex_t *mutex)
{
atomic_init(&mutex->locked, false);
}
void my_mutex_lock(my_mutex_t *mutex)
{
while (atomic_exchange(&mutex->locked, true))
; // spin until the lock is acquired
}
void my_mutex_unlock(my_mutex_t *mutex)
{
atomic_store(&mutex->locked, false);
}
int buffer_size = 100;
vector<int> product_array(buffer_size, 0);
my_mutex_t sema1;
my_mutex_t sema2;
my_mutex_t sema3;
atomic<int> check1 = 0;
atomic<int> check2 = buffer_size;
int size_ = 0;
int left_ = 0;
int right_ = 0;
void producer()
{
while (sema2.locked)
;
if (++check1 == buffer_size)
{
my_mutex_lock(&sema2);
}
while (sema1.locked)
;
my_mutex_lock(&sema1);
cout << "Producer entered the critcal section\n";
cout << "producer is producing\n Products left \n";
size_++;
product_array[right_ % buffer_size] = rand();
for (auto it : product_array)
{
if (it != 0)
{
cout << it << " ";
}
}
cout << endl;
right_++;
right_ = right_ % buffer_size;
my_mutex_unlock(&sema1);
my_mutex_unlock(&sema3);
}
void consumer()
{
while (sema3.locked)
;
if (++check2 == buffer_size)
{
my_mutex_lock(&sema3);
}
while (sema1.locked)
;
my_mutex_lock(&sema1);
cout << "Consumer entered the critcal section\n";
cout << "Consumer is consuming\n Products left \n";
size_--;
product_array[left_ % buffer_size] = 0;
left_++;
left_ = left_ % buffer_size;
for (auto it : product_array)
{
if (it != 0)
{
cout << it << " ";
}
}
cout << endl;
my_mutex_unlock(&sema1);
my_mutex_unlock(&sema2);
}
int main()
{
int n1 = 10;
int n2 = 5;
pthread_t pro[n1], cons[n2];
my_mutex_init(&sema1);
my_mutex_init(&sema2);
my_mutex_init(&sema3);
my_mutex_lock(&sema3);
for (int i = 0; i < n1; i++)
{
pthread_create(&pro[i], NULL, (void *(*)(void *))producer, NULL);
}
for (int i = 0; i < n2; i++)
{
pthread_create(&cons[i], NULL, (void *(*)(void *))consumer, NULL);
}
for (int i = 0; i < n1; i++)
{
pthread_join(pro[i], NULL);
}
for (int i = 0; i < n2; i++)
{
pthread_join(cons[i], NULL);
}
return 0;
}