Advertisement
frasl

Untitled

Feb 28th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. template <typename RandomIt>
  2. int parallel_sum(RandomIt beg, RandomIt end)
  3. {
  4.     auto len = end - beg;
  5.     if (len < 1000)
  6.         return std::accumulate(beg, end, 0);
  7.  
  8.     RandomIt mid = beg + len / 2;
  9.     auto res = std::async(std::launch::async | std::launch::deferred,
  10.         parallel_sum<RandomIt>, mid, end);
  11.     int sum = parallel_sum(beg, mid);
  12.     return sum + res.get();
  13. }
  14.  
  15. int main()
  16. {
  17.     std::vector<int> v(10000, 1);
  18.     std::cout << "The sum is " << parallel_sum(v.begin(), v.end()) << '\n';
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement