gpt4 book ai didi

c++ - 是否有 "single mutex deadlock"的术语(非递归互斥的死锁类型情况)?

转载 作者:行者123 更新时间:2023-11-30 01:38:22 28 4
gpt4 key购买 nike

由于多次调用获取非递归互斥体,以下代码挂起:

#include <pthread.h>

class Lock
{
public:

Lock( pthread_mutex_t& mutex )
: mutex_( mutex )
{
pthread_mutex_lock( &mutex_ );
}

~Lock()
{
pthread_mutex_unlock( &mutex_ );
}

private:

pthread_mutex_t& mutex_;
};

class Foo
{
public:

Foo()
{
pthread_mutex_init( &mutex_, NULL );
}

~Foo()
{
pthread_mutex_destroy( &mutex_ );
}

void hang()
{
Lock l( mutex_ );
subFunc();
}

void subFunc()
{
Lock l( mutex_ );
}

private:

pthread_mutex_t mutex_;
};

int main()
{
Foo f;
f.hang();
}

有没有形容这种情况的词或短语?我不确定,但我不认为这可以称为死锁:我的理解是死锁本身指的是因无法按顺序获取多个共享资源而导致的僵局.

我一直称其为“单互斥死锁”,但我想知道是否有更合适的术语/短语。

最佳答案

The Wikipedia article on reentrant mutexes引用 Pattern-Oriented Software Architecture,它使用术语“自死锁”。这个词对我来说似乎很合理!

...mutexes come in two basic flavors: recursive and non-recursive. A recursive mutex allows re-entrant locking, in which a thread that has already locked a mutex can lock it again and progress. Non-recursive mutexes, in contrast, cannot: a second lock in the same thread results in self-deadlock. Non-recursive mutexes can potentially be much faster to lock and unlock than recursive mutexes, but the risk of self-deadlock means that care must be taken when an object calls any methods on itself, either directly or via a callback, because double-locking will cause the thread to hang.

(强调)

跨各种技术的各种搜索结果证实了该术语的使用。

关于c++ - 是否有 "single mutex deadlock"的术语(非递归互斥的死锁类型情况)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48040198/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com