fork download
  1. #include <iostream>
  2. #include <future>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6. #define LOG() std::cout << __func__ << " : "
  7.  
  8. void test()
  9. {
  10. LOG() << "IN\n";
  11.  
  12. using namespace std::chrono_literals;
  13. std::this_thread::sleep_for( 1s );
  14.  
  15. LOG() << "OUT\n";
  16. }
  17.  
  18. int main()
  19. {
  20. LOG() << "Calling test()...\n";
  21.  
  22. auto f = std::async( std::launch::async, test );
  23.  
  24. LOG() << "Running test()...\n";
  25.  
  26. // ... ...
  27. // ... You can do other stuff here ...
  28. // ... ...
  29.  
  30. f.wait(); // Blocking call to wait for the result to be available
  31.  
  32. LOG() << "Exiting...\n";
  33.  
  34. return 0;
  35. }
Success #stdin #stdout 0s 4244KB
stdin
Standard input is empty
stdout
main : Calling test()...
main : Running test()...
test : IN
test : OUT
main : Exiting...