fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <thread>
  4. #include <mutex>
  5.  
  6. thread_local unsigned int rage = 1;
  7. std::mutex cout_mutex;
  8.  
  9. void increase_rage(const std::string& thread_name)
  10. {
  11. ++rage;
  12. std::lock_guard<std::mutex> lock(cout_mutex);
  13. std::cout << "Rage counter for " << thread_name << ": " << rage << '\n';
  14. }
  15.  
  16. int main()
  17. {
  18. std::thread a(increase_rage, "a"), b(increase_rage, "b");
  19.  
  20. {
  21. std::lock_guard<std::mutex> lock(cout_mutex);
  22. std::cout << "Rage counter for main: " << rage << '\n';
  23. }
  24.  
  25. a.join();
  26. b.join();
  27.  
  28. return 0;
  29. }
Runtime error #stdin #stdout #stderr 0s 3428KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted