Advertisement
rhornig

Untitled

Feb 22nd, 2023
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4.  
  5. std::recursive_mutex my_mutex;
  6.  
  7. void recursive_function(int n) {
  8. std::lock_guard<std::recursive_mutex> lock(my_mutex);
  9. std::cout << "Thread " << n << " has acquired the lock." << std::endl;
  10.  
  11. if (n > 0) {
  12. recursive_function(n-1);
  13. }
  14.  
  15. std::cout << "Thread " << n << " is releasing the lock." << std::endl;
  16. }
  17.  
  18. int main() {
  19. std::thread t1(recursive_function, 3);
  20. std::thread t2(recursive_function, 2);
  21.  
  22. t1.join();
  23. t2.join();
  24.  
  25. return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement