fork download
  1. #include <cstdio>
  2. #include <numeric>
  3. #include <thread>
  4. #include <vector>
  5.  
  6. void p2()
  7. {
  8. std::vector<int> data{};
  9. data.resize(10000);
  10. std::iota(data.begin(), data.end(), 0);
  11.  
  12. unsigned long sum = 0;
  13. unsigned long gre = 0;
  14.  
  15. std::thread t1{[&]() {
  16. for(const auto &num: data)
  17. sum += num;
  18. }};
  19. std::thread t2{[&]() {
  20. for(const auto &num: data)
  21. {
  22. if(num > 20)
  23. ++gre;
  24. }
  25. }};
  26.  
  27. t1.join();
  28. t2.join();
  29.  
  30. fprintf(
  31. stdout,
  32. "Sum: %u\nGreater than 20: %u\n",
  33. sum, gre
  34. );
  35. }
  36.  
  37. int main(int, const char **)
  38. {
  39. p2();
  40. return 0;
  41. }
Success #stdin #stdout 0s 19800KB
stdin
Standard input is empty
stdout
Sum: 49995000
Greater than 20: 9979