fork download
  1. #include <iostream>
  2. #include <thread>
  3. #include <vector>
  4. #include <atomic>
  5.  
  6. std::atomic_flag spinlock = ATOMIC_FLAG_INIT;
  7.  
  8. void thread_func()
  9. {
  10. while(spinlock.test_and_set());
  11. std::cout << "thread_func() printing!" << std::endl;
  12. spinlock.clear();
  13. }
  14.  
  15.  
  16. int main()
  17. {
  18. spinlock.test_and_set();
  19.  
  20. std::vector<std::thread> threads;
  21. for(unsigned int i = 0; i < 10; ++i)
  22. threads.emplace_back(thread_func);
  23. spinlock.clear();
  24.  
  25. for(auto& t : threads)
  26. t.join();
  27. }
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