#include #include #include using namespace std ; recursive_mutex recursiveMutex ; int shared_resource = 0; void recursiveFunction(int depth) { if (depth <= 0) { return; } recursiveMutex.lock(); // Acquire the lock cout << "Thread " << this_thread::get_id() << " acquired lock at depth " << depth << endl; shared_resource++; // Access shared resource recursiveFunction(depth - 1); // Recursive call, re-acquiring the lock cout << "Thread " << this_thread::get_id() << " releasing lock at depth " << depth << endl; recursiveMutex.unlock(); // Release the lock } int main() { thread t1(recursiveFunction, 2); thread t2(recursiveFunction, 2); t1.join(); t2.join(); cout << "Final shared_resource value: " << shared_resource << endl; return 0; }