fork download
  1. #include <bits/stdc++.h>
  2. #include <thread>
  3. using namespace std;
  4. using namespace std::chrono;
  5.  
  6.  
  7. int oddSum=0;
  8. int evenSum=0;
  9.  
  10.  
  11. void findOddSum(int start,int end){
  12. for(int i=start;i<=end;i++){
  13. if(i%2)
  14. oddSum+=i;
  15. }
  16. }
  17.  
  18. void findEvenSum(int start,int end){
  19. for(int i=start;i<=end;i++){
  20. if(i%2==0)
  21. evenSum+=i;
  22. }
  23. }
  24.  
  25.  
  26. int32_t main(){
  27.  
  28. auto startTime=high_resolution_clock::now();
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44. thread t1(findEvenSum,1,1e9);
  45. thread t2(findOddSum,1,1e9);
  46.  
  47. t1.join();
  48. t2.join();
  49.  
  50. cout<<oddSum<<endl;
  51. cout<<evenSum<<endl;
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. auto stopTime=high_resolution_clock::now();
  74. auto duration=duration_cast<microseconds>(stopTime-startTime);
  75.  
  76. cout<<"Seconds : "<<duration.count()/1e6<<endl;
  77.  
  78.  
  79.  
  80. }
  81.  
  82.  
  83.  
Success #stdin #stdout 1.48s 5500KB
stdin
Standard input is empty
stdout
-371654656
128345344
Seconds : 1.48173