fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4.  
  5. using namespace std;
  6.  
  7. int count = 0;
  8. std::mutex mtx;
  9.  
  10. void foo(){
  11. for(int i = 0; i < 100000000; i++){
  12. // std::lock_guard<std::mutex> lock(mtx);
  13. count++;
  14. }
  15. }
  16.  
  17. int main(){
  18. std::thread t1(foo);
  19. std::thread t2(foo);
  20. std::thread t3(foo);
  21. std::thread t4(foo);
  22.  
  23. t1.join();
  24. t2.join();
  25. t3.join();
  26. t4.join();
  27.  
  28. cout << count << endl;
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
400000000