-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuture.cpp
37 lines (33 loc) · 1.11 KB
/
future.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
#include <future>
#include <thread>
#include <vector>
#include <iostream>
void f(std::promise<long long>& p, int begin, int end) {
long long sum{};
for (int value = begin; value != end; ++value) {
sum += value + 1;
}
p.set_value(sum);
}
int main() {
const int sumSize = 1'000'000'000;
unsigned threadCount = (std::thread::hardware_concurrency() < 2) ? 1 : std::thread::hardware_concurrency();
std::vector<std::promise<long long>> promises(threadCount);
std::vector<std::future<long long>> futures(threadCount);
std::vector<std::thread> threads;
int portionBegin{}, portionEnd{};
for (int t = 0; t != threadCount; ++t) {
portionEnd = portionBegin + sumSize / threadCount;
threads.emplace_back(f, std::ref(promises.at(t)), portionBegin, portionEnd);
portionBegin = portionEnd;
futures.at(t) = promises.at(t).get_future();
}
for (auto& t : threads) {
t.join();
}
long long sum{};
for (auto& f : futures) {
sum += f.get();
}
std::cout << "Sum of first " << sumSize << " numbers is: " << sum << '\n';
}