Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <typename RandomIt>
- int parallel_sum(RandomIt beg, RandomIt end)
- {
- auto len = end - beg;
- if (len < 1000)
- return std::accumulate(beg, end, 0);
- RandomIt mid = beg + len / 2;
- auto res = std::async(std::launch::async | std::launch::deferred,
- parallel_sum<RandomIt>, mid, end);
- int sum = parallel_sum(beg, mid);
- return sum + res.get();
- }
- int main()
- {
- std::vector<int> v(10000, 1);
- std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement