-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (59 loc) · 2.02 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (59 loc) · 2.02 KB
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
#include <iostream>
#include <chrono>
#include "src/ThreadPool.h"
using std::cout, std::cin;
std::mutex mtx;
int main() {
ThreadPool<int> tp{10};
vector<int> v = {1, 24, 3, 4, 5, 73, 15};
function<void(vector<int>)> f_sum = [](vector<int> a) {
int res{};
for (const auto &el: a) {
res += el;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::unique_lock<std::mutex> ul{mtx};
cout << "sum thread_id " << std::this_thread::get_id() << " res = " << res << '\n';
ul.unlock();
std::this_thread::sleep_for(std::chrono::seconds(3));
};
function<void(vector<int>)> f_square = [](vector<int> a) {
int res{};
for (const auto &el: a) {
res += el * el;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::unique_lock<std::mutex> ul{mtx};
cout << "square thread_id " << std::this_thread::get_id() << " res = " << res << '\n';
ul.unlock();
std::this_thread::sleep_for(std::chrono::seconds(3));
};
function<void(vector<int>)> f_module_4 = [](vector<int> a) {
int res{};
for (const auto &el: a) {
res += el%4;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::unique_lock<std::mutex> ul{mtx};
cout << "module 4 thread_id " << std::this_thread::get_id() << " res = " << res % 4 << '\n';
ul.unlock();
std::this_thread::sleep_for(std::chrono::seconds(3));
};
int num_func[3];
cout << "Enter number of sum function:\n";
cin >> num_func[0];
cout << "Enter number of power function:\n";
cin >> num_func[1];
cout << "Enter number of sum function:\n";
cin >> num_func[2];
for (int i{}; i < num_func[0]; i++) {
tp.add_task(f_sum,v);
}
for (int i{}; i < num_func[1]; i++) {
tp.add_task(f_square,v);
}
for (int i{}; i < num_func[2]; i++) {
tp.add_task(f_module_4,v);
}
return 0;
}