#include #include #include using namespace std ; recursive_mutex myRecursiveMutex; void recursiveFunction(int depth) { unique_lock lock(myRecursiveMutex); // Acquires the lock cout << "Thread " << this_thread::get_id() << " entered recursiveFunction at depth " << depth << endl; if (depth > 0) { recursiveFunction(depth - 1); // Recursive call, re-acquires the lock } cout << "Thread " << this_thread::get_id() << " exiting recursiveFunction at depth " << depth << endl; } // Lock is released here (when unique_lock goes out of scope) int main() { thread t1(recursiveFunction, 2); t1.join(); return 0; }