-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
165 lines (120 loc) · 3.84 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "painter.h"
#include "clearing_helper.h"
#include "filling_helper.h"
#include "wine_helper.h"
#include <vector>
#include "global.h"
using namespace std;
void *painterStart(void *p);
void *fillingHelperStart(void *p);
void *clearingHelperStart(void *p);
void *wineHelperStart(void *p);
int main(int argc, const char * argv[])
{
int i;
int painters_count;
if (argc==1)
painters_count = 10;
else
painters_count = atoi(argv[1]);
if (painters_count < 4) {
printf(ANSI_COLOR_RED "Number of painters must be >= 4!\n" ANSI_COLOR_RESET);
exit(2);
}
if (painters_count % 2 != 0) {
printf(ANSI_COLOR_RED "Number of painters is not even!\n" ANSI_COLOR_RESET);
exit(2);
}
brush_count = painters_count / 2;
std::vector<pthread_t> t_painters;
std::vector<pthread_t> t_helpers;
// Reserve memory for global variables
t_painters.reserve(painters_count);
t_helpers.reserve(3);
pthread_attr_t attr;
paint_mutex.reserve(brush_count);
paint_cond.reserve(brush_count);
brush_mutex.reserve(brush_count);
brush_cond.reserve(brush_count);
paint_count.reserve(brush_count);
brush_usage_side.reserve(brush_count);
pthread_mutex_init(&wine_mutex, NULL);
pthread_cond_init(&wine_cond, NULL);
wine_count = 4;
for (i = 0; i < brush_count; i++) {
pthread_mutex_init(&paint_mutex[i], NULL);
pthread_mutex_init(&brush_mutex[i], NULL);
pthread_cond_init(&paint_cond[i], NULL);
pthread_cond_init(&brush_cond[i], NULL);
paint_count[i] = 3;
brush_usage_side[i] = 0;
}
// Initialize painters
std::vector<Painter> painters;
painters.reserve(painters_count);
painters.push_back(Painter(0, 0, brush_count-1));
int brush_ind;
int paint_ind;
for (i = 1; i < painters_count; i++) {
paint_ind = i/2;
if (i % 2 == 0) {
brush_ind = i/2 - 1;
} else {
brush_ind = i/2;
}
painters.push_back(Painter(i, paint_ind, brush_ind));
}
pthread_attr_init(&attr);
// Start helpers
pthread_create(t_helpers.data(), &attr, &clearingHelperStart, NULL);
pthread_create(t_helpers.data() + 1, &attr, &fillingHelperStart, NULL);
pthread_create(t_helpers.data() + 2, &attr, &wineHelperStart, NULL);
// Start painters
for(i = 0; i < painters_count; i++)
{
pthread_create(t_painters.data() + i, &attr, &painterStart, painters.data() +i);
}
// Join painters threads so that program stops
for (i = 0; i < painters_count; i++)
{
pthread_join(t_painters[i], NULL);
}
pthread_attr_destroy(&attr);
return 0;
}
void *painterStart(void* p)
{
srand(time(NULL));
Painter *obj = (Painter*)p;
obj->work();
pthread_exit(NULL);
}
void *fillingHelperStart(void *p) {
srand(time(NULL));
FillingHelper *fillingObj = new FillingHelper(brush_count);
printf(ANSI_COLOR_YELLOW "[Helper] Filling Helper starts working\n" ANSI_COLOR_RESET);
fillingObj->work();
printf(ANSI_COLOR_YELLOW "Finished!" ANSI_COLOR_YELLOW);
pthread_exit(NULL);
}
void *clearingHelperStart(void *p) {
srand(time(NULL));
ClearingHelper *clearingObj = new ClearingHelper(brush_count);
printf(ANSI_COLOR_YELLOW "[Helper] Clearing Helper starts working\n" ANSI_COLOR_RESET);
clearingObj->work();
printf(ANSI_COLOR_YELLOW "Finished!" ANSI_COLOR_RESET);
pthread_exit(NULL);
}
void *wineHelperStart(void *p) {
srand(time(NULL));
WineHelper *wineObj = new WineHelper();
printf(ANSI_COLOR_YELLOW "[Helper] Wine Helper starts working\n" ANSI_COLOR_RESET);
wineObj->work();
printf(ANSI_COLOR_YELLOW "Finished!" ANSI_COLOR_RESET);
pthread_exit(NULL);
}