fork(6) download
  1.  
  2. #include <thread>
  3. #include <mutex>
  4. #include <iostream>
  5. #include <chrono>
  6. std::mutex m1,m2;
  7. void foo() {
  8. m1.lock();
  9. std::cout<<"foo locked m1"<<std::endl;
  10. std::this_thread::sleep_for (std::chrono::seconds(1));
  11. m2.lock();
  12. m1.unlock();
  13. std::cout<<"foo locked m2 and releases m1"<<std::endl;
  14. m2.unlock();
  15. std::cout<<"foo is ok"<<std::endl;
  16. }
  17. void bar() {
  18. m2.lock();
  19. std::cout<<"bar locked m2"<<std::endl;
  20. std::this_thread::sleep_for (std::chrono::seconds(1));
  21. m1.lock();
  22. m2.unlock();
  23. std::cout<<"barlocked m1 and releases m2"<<std::endl;
  24. m1.unlock();
  25. std::cout<<"bar is ok"<<std::endl;
  26. }
  27. int main()
  28. {
  29. std::thread t1(foo);
  30. bar();
  31. t1.join();
  32. std::cout << "Everything went fine"<<std::endl;
  33. return 0;
  34. }
Time limit exceeded #stdin #stdout 5s 5520KB
stdin
Standard input is empty
stdout
bar locked m2
foo locked m1