Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <mutex>
- std::recursive_mutex my_mutex;
- void recursive_function(int n) {
- std::lock_guard<std::recursive_mutex> lock(my_mutex);
- std::cout << "Thread " << n << " has acquired the lock." << std::endl;
- if (n > 0) {
- recursive_function(n-1);
- }
- std::cout << "Thread " << n << " is releasing the lock." << std::endl;
- }
- int main() {
- std::thread t1(recursive_function, 3);
- std::thread t2(recursive_function, 2);
- t1.join();
- t2.join();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement