[英]std::shared_mutex recursive protection by thread_local storage
The idea is to use the std::shared_mutex
all right, but protect deadlocks in case that std::shared_mutex::lock()
for exclusive access is called by the same thread.这个想法是可以使用
std::shared_mutex
,但在同一线程调用用于独占访问的std::shared_mutex::lock()
情况下保护死锁。
For example:例如:
std::shared_mutex m;
void f2()
{
m.lock();
}
void f1()
{
m.lock();
f2();
}
f1()
would lock because std::shared_mutex cannot be called recursively. f1()
会锁定,因为 std::shared_mutex 不能递归调用。 For this I have two options: Either to use my own tlock of read-write mutex which uses Windows Mutex which supports recursive locking, or to use this sort of a locking function based on a thread_local
variable:为此,我有两个选择:要么使用我自己的读写互斥锁tlock ,它使用支持递归锁定的 Windows Mutex,要么使用这种基于
thread_local
变量的锁定函数:
thread_local bool LockedStuff = 0;
void Lock(std::function<...> f)
{
if (LockedStuff)
{
f(...);
return;
}
LockedStuff = 1;
m.lock();
f(...);
m.unlock();
LockedLinks = 0;
}
The question is, is there anything I'm missing that can go wrong with the above implementation?问题是,上面的实现有什么我遗漏的地方吗? Thanks a lot.
非常感谢。
That you don't RAII your mutex and thread local variable.你没有 RAII 你的互斥锁和线程局部变量。 If exception is called inside the function your mutex will be forever locked.
如果在函数内部调用异常,您的互斥锁将永远被锁定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.