fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8. void example1(){ for(int i = 0; i < 10; ++i) { cout << 1; this_thread::sleep_for(100ms);} }
  9. void example2(){ for(int i = 0; i < 10; ++i) { cout << 2; this_thread::sleep_for(100ms);} }
  10. void start_threads()
  11. {
  12.  
  13. thread t1(example1);
  14. thread t2(example2);
  15.  
  16. t1.join();
  17. t2.join();
  18.  
  19. cout << endl;
  20. }
  21.  
  22. int main(int argc, const char * argv[])
  23. {
  24. start_threads();
  25. cout << "Once more...\n";
  26. start_threads();
  27. }
  28.  
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
21212121212121212121
Once more...
21212121212121212121