我有一个案例,我的算法决策基于共享的深度std::recursive_mutex
。
#include <iostream>
#include <mutex>
#include <thread>
int g_i = 0;
std::recursive_mutex g_i_mutex;
void bar() {
std::lock_guard<std::recursive_mutex> lock(g_i_mutex);
switch (get_counter(g_i_mutex)) { // some way to find the number of owners
case 1: std::cout << "depth 1\n"; break;
case 2: std::cout << "depth 2\n"; break;
default:;
}
}
void foo() {
std::lock_guard<std::recursive_mutex> lock(g_i_mutex);
std::cout << "hello\n";
bar();
}
int main() {
foo(); //print hello then depth 2
bar(); //print depth 1
}
我读过递归互斥锁持有某种使用计数,并且每次调用锁定/解锁时都会增加和减少它,有没有办法访问该信息?