-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_function_pointer.cpp
More file actions
35 lines (27 loc) · 1007 Bytes
/
thread_function_pointer.cpp
File metadata and controls
35 lines (27 loc) · 1007 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
33
34
35
#include <iostream>
#include <vector>
#include <thread>
#include <algorithm>
#include <cstdlib>
void accumulator_function2(const std::vector<int> &v, unsigned long long &acm, unsigned int beginIndex, unsigned int endIndex) {
acm = 0;
for (unsigned int i = beginIndex; i < endIndex; ++i) {
acm += v[i];
}
}
int main() {
unsigned long long acm1 = 0;
unsigned long long acm2 = 0;
std::vector<int> v(1024);
srand(time(nullptr));
std::generate(v.begin(), v.end(), [&v]() { return rand() % v.size(); } );
for_each(begin(v), end(v), [](int i) { std::cout << i << ' '; } );
std::thread t1(accumulator_function2, std::ref(v), std::ref(acm1), 0, v.size() / 2);
std::thread t2(accumulator_function2, std::ref(v), std::ref(acm2), v.size() / 2, v.size());
t1.join();
t2.join();
std::cout << "acm1: " << acm1 << std::endl;
std::cout << "acm2: " << acm2 << std::endl;
std::cout << "acm1 + acm2: " << acm1 + acm2 << std::endl;
return 0;
}