fork download
  1. #include <cstdlib>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <thread>
  5. #include <future>
  6.  
  7. int main()
  8. {
  9. std::vector<int> v(1ul<<27);
  10.  
  11. auto const chunk = v.size()/4;
  12.  
  13. auto f(begin(v)), l(end(v));
  14. std::generate(f, l, rand);
  15.  
  16. #if 0
  17. auto t1 = std::thread([&] () mutable { std::sort(f+chunk*0,f+chunk*1); } );
  18. auto t2 = std::thread([&] () mutable { std::sort(f+chunk*1,f+chunk*2); } );
  19. auto t3 = std::thread([&] () mutable { std::sort(f+chunk*2,f+chunk*3); } );
  20. auto t4 = std::thread([&] () mutable { std::sort(f+chunk*3,l ); } );
  21. t1.join();
  22. t2.join();
  23. t3.join();
  24. t4.join();
  25. #else
  26. auto f1 = std::async(std::launch::async, [&] () mutable { std::sort(f+chunk*0,f+chunk*1); } );
  27. auto f2 = std::async(std::launch::async, [&] () mutable { std::sort(f+chunk*1,f+chunk*2); } );
  28. auto f3 = std::async(std::launch::async, [&] () mutable { std::sort(f+chunk*2,f+chunk*3); } );
  29. auto f4 = std::async(std::launch::async, [&] () mutable { std::sort(f+chunk*3,l ); } );
  30. f1.get();
  31. f2.get();
  32. f3.get();
  33. f4.get();
  34. #endif
  35.  
  36. std::inplace_merge(f,f+chunk*1,f+chunk*2);
  37. std::inplace_merge(f+chunk*2,f+chunk*3,l);
  38. std::inplace_merge(f,f+chunk*2,l);
  39. }
  40.  
Runtime error #stdin #stdout 3.86s 527360KB
stdin
02:43 AM/tmp % make -B && time ./test
g++ -std=c++0x -g -O3 -I ~/custom/boost/ -march=native test.cpp -o test -lpthread

real	0m5.191s
user	0m12.601s
sys	0m0.244s
stdout
Standard output is empty