fork download
  1. #include <iostream>
  2. #include <thread>
  3.  
  4. void threadFunction(int id) {
  5. std::cout << "Thread " << id << " is running." << std::endl;
  6. }
  7.  
  8. int main() {
  9. const int numThreads = 4;
  10. std::thread threads[numThreads];
  11.  
  12. for (int i = 0; i < numThreads; i++) {
  13. threads[i] = std::thread(threadFunction, i);
  14. }
  15.  
  16. for (int i = 0; i < numThreads; i++) {
  17. threads[i].join();
  18. }
  19.  
  20. return 0;
  21. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Thread 3 is running.
Thread 2 is running.
Thread 1 is running.
Thread 0 is running.