fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4. #include <numeric>
  5. #include <future>
  6. #include <algorithm>
  7.  
  8. int sum(const int * a, const int * b)
  9. {
  10. std::cout << std::this_thread::get_id() << '\n';
  11.  
  12. auto distance = b-a;
  13. if(distance < 5) // willkuerliche Grenze
  14. return std::accumulate(a,b,0);
  15.  
  16. const int * half = a + distance/2;
  17.  
  18. auto fut_left = std::async(sum, a, half);
  19. auto right = sum(half, b);
  20.  
  21. return fut_left.get() + right;
  22. }
  23.  
  24. int main()
  25. {
  26. std::vector<int> vec(256);
  27. std::iota(vec.begin(), vec.end(), 0);
  28. std::cout << sum(vec.data(), vec.data()+256) << '\n';
  29. }
  30.  
Runtime error #stdin #stdout #stderr 0s 3484KB
stdin
Standard input is empty
stdout
thread::id of a non-executing thread
thread::id of a non-executing thread
thread::id of a non-executing thread
thread::id of a non-executing thread
thread::id of a non-executing thread
thread::id of a non-executing thread
thread::id of a non-executing thread
stderr
terminate called after throwing an instance of 'std::system_error'
  what():  Unknown error -1