-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequential_withThreads.cpp
70 lines (58 loc) · 1.78 KB
/
sequential_withThreads.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
#include <iostream>
#include <chrono>
#include <vector>
#include <thread>
#include <cstdlib>
using namespace std;
void init_rand_numbers(vector<int>&);
unsigned long long int sum_vector(vector<int>&);
unsigned long long int show_result(vector<int> v, unsigned long long int&);
int main(){
const long SIZE = 50000000;
vector<int> a(SIZE);
vector<int> b(SIZE);
vector<int> c(SIZE);
vector<int> d(SIZE);
vector<int> e(SIZE);
unsigned long long int result1, result2, result3, result4, result5;
init_rand_numbers(a);
init_rand_numbers(b);
init_rand_numbers(c);
init_rand_numbers(d);
init_rand_numbers(e);
auto start = chrono::steady_clock::now();
thread t1(show_result, cref(a), ref(result1));
thread t2(show_result, cref(b), ref(result2));
thread t3(show_result, cref(c), ref(result3));
thread t4(show_result, cref(d), ref(result4));
thread t5(show_result, cref(e), ref(result5));
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
auto end = chrono::steady_clock::now();
cout << "Sum of Vector A: " << result1 << endl;
cout << "Sum of Vector B: " << result2 << endl;
cout << "Sum of Vector C: " << result3 << endl;
cout << "Sum of Vector D: " << result4 << endl;
cout << "Sum of Vector E: " << result5 << endl;
cout << chrono::duration_cast<chrono::milliseconds>(end - start).count() << "ms" << endl;
return 0;
}
void init_rand_numbers(vector<int>& v) {
for(int & i : v) {
i = rand();
}
}
unsigned long long int sum_vector(vector<int>& array) {
int sum = 0;
for(auto element : array) {
sum+=element;
}
return sum;
}
unsigned long long int show_result(vector<int> v, unsigned long long int& result) {
result = sum_vector(v);
return result;
}