fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <thread>
  4.  
  5. using namespace std;
  6.  
  7. void print()
  8. {
  9. cout<<"Hello!!";
  10. }
  11.  
  12. void print2()
  13. {
  14. for(int i = 0; i < 8; ++i)
  15. {
  16. cout << "meanwhile...\n";
  17. this_thread::sleep_for(chrono::milliseconds(200));
  18. }
  19. }
  20.  
  21. thread test()
  22. {
  23. this_thread::sleep_for(chrono::milliseconds(200));
  24. return thread([&]() {
  25. this_thread::sleep_for(chrono::milliseconds(400));
  26. cout << "Connection is successful! Wait...\n";
  27. this_thread::sleep_for(chrono::milliseconds(400));
  28. cout << "Setting up the environment!\n";
  29. this_thread::sleep_for(chrono::milliseconds(400));
  30. cout <<"All is done!\n";});
  31. }
  32.  
  33. int main(int argc, char * argv[])
  34. {
  35. auto t = test();
  36. print2();
  37. t.join();
  38. print();
  39. }
  40.  
Success #stdin #stdout 0.01s 5432KB
stdin
Standard input is empty
stdout
meanwhile...
meanwhile...
Connection is successful! Wait...
meanwhile...
meanwhile...
Setting up the environment!
meanwhile...
meanwhile...
All is done!
meanwhile...
meanwhile...
Hello!!