-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
32 lines (25 loc) · 712 Bytes
/
Main.cpp
File metadata and controls
32 lines (25 loc) · 712 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
#include "Consumer.h"
#include "Producer.h"
#include "Utility.h"
#include <algorithm>
#include <array>
#include <thread>
#include <vector>
static cus numOfProducers = 5;
static cus numOfConsumers = 5;
int main(int argc, char* argv[])
{
std::array<std::thread, numOfProducers + numOfConsumers> workers;
std::shared_ptr<Data> data(std::make_shared<Data>());
Producer p(data);
us i = 0;
for (; i < numOfProducers; ++i) {
workers[i] = std::thread(&Producer::produce, &p, i + 1);
}
Consumer c(data);
for (; i < numOfProducers + numOfConsumers; ++i) {
workers[i] = std::thread(&Consumer::consume, &c, i + 1);
}
std::for_each(workers.begin(), workers.end(), [](auto& w) { w.join(); });
return 0;
}