fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4.  
  5. int main() {
  6. std::cout << "Hello" << std::endl;
  7. int n = 0;
  8. std::cin >> n;
  9. std::vector<std::thread> threads;
  10.  
  11. std::cout << "Loop for " << n << std::endl;
  12.  
  13. for(int i = 0 ; i < n ; i++) {
  14. threads.emplace_back([](int p){
  15. std::cout << p << "=" << p*p << std::endl;
  16. },i);
  17. }
  18.  
  19. for(std::thread& thread : threads) {
  20. thread.join();
  21. }
  22.  
  23. std::cout << "Bye" << std::endl;;
  24. return 0;
  25. }
Runtime error #stdin #stdout #stderr 0s 3480KB
stdin
10
stdout
Hello
Loop for 10
stderr
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted