fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <chrono>
  4. using namespace std;
  5.  
  6. // You need something to signal your thread that it should exit.
  7. // So, we're using a global variable to store this information.
  8.  
  9. volatile bool is_exited = false;
  10.  
  11. // Here is our callback() or long running task that
  12. // our threads would be executing
  13.  
  14. void callback()
  15. {
  16. // This loop should exit once is_exited flag's condition is true.
  17. // This loop body is actually indicating the actual job that the
  18. // thread needs to execute.
  19.  
  20. auto id = this_thread::get_id();
  21.  
  22. cout << id << ". Started!\n";
  23.  
  24. while( !is_exited )
  25. {
  26. cout << id << ". Hello!\n";
  27. this_thread::sleep_for( 100ms );
  28. }
  29.  
  30. cout << id << ". Exited!\n";
  31. }
  32.  
  33. // Here is our main() function that would start two threads,
  34. // register the callback, and run it. After doing its own processing,
  35. // the main() function would signal threads to stop and wait for them
  36. // to finish their running task and get out of the callbacks. Usually,
  37. // there's a join() or wait() function call to do this. C++ has join().
  38.  
  39. int main()
  40. {
  41. cout << "main() started!\n";
  42.  
  43. // Now, register your callback with your threads
  44. // C++ threads start right away after this
  45. // You don't have to call some start() function
  46.  
  47. thread t1 { callback };
  48. thread t2 { callback };
  49.  
  50. // So, now that the threads have started the main()
  51. // can do its own things
  52.  
  53. //for ( int i = 0; i < 100000000; ++i )
  54. //{
  55. // // ...
  56. //}
  57.  
  58. this_thread::sleep_for( 1s );
  59.  
  60. // Now, we need to send the signal to threads to stop their
  61. // execution for a graceful termination.
  62.  
  63. is_exited = true;
  64.  
  65. // There's a call to join() function to wait for the
  66. // threads to exit; so here the main() function is waiting
  67. // for the threads to exit. Once the callback() finishes,
  68. // the thread would exit and the main() function would get out
  69. // of these join() calls. And, the program would exit gracefully.
  70.  
  71. t1.join();
  72. t2.join();
  73.  
  74. cout << "main() exited!\n";
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 0s 19344KB
stdin
Standard input is empty
stdout
main() started!
47277466478336. Started!
47277466478336. Hello!
47277464377088. Started!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Hello!
47277464377088. Hello!
47277466478336. Exited!
47277464377088. Exited!
main() exited!