我在下面的代码中有一个互斥死锁:
CRegistry::CRegistry()
{
pthread_mutex_init(&_Mutex, NULL);
}
CRegistry::~CRegistry()
{
pthread_mutex_destroy(&_Mutex);
}
MR_RESULT CRegistry::Register(const REGISTRY_KEY &Id, const REGISTRY_ITEM &Item)
{
pair<REGISTRY::iterator, bool> Result;
pthread_mutex_lock(&_Mutex);
Result = _Registry.insert(pair<REGISTRY_KEY, REGISTRY_ITEM>(Id, Item));
pthread_mutex_unlock(&_Mutex);
if (Result.second == true)
return MR_RESULT_OK;
else
return MR_RESULT_ERROR;
}
MR_RESULT CRegistry::UnRegister(const REGISTRY_KEY &Id)
{
REGISTRY::size_type Result;
pthread_mutex_lock(&_Mutex);
Result = _Registry.erase(Id);
pthread_mutex_unlock(&_Mutex);
if (Result == 1)
return MR_RESULT_OK;
else
return MR_RESULT_ERROR;
}
_Mutext
是一个类成员,没有在代码的任何其他地方使用。在某些时候,我可以看到一个线程卡住了,试图锁定已经锁定的互斥量。
锁定互斥量的线程有实时线程和非实时线程。我知道可能存在优先级倒置,但这怎么会导致死锁呢?
最佳答案
我觉得你的代码非常好。您确定 _Mutex 没有在其他任何地方使用吗?
Valgrind 工具集包括 Helgrind ,一个可以帮助您消除死锁的 pthread 调试器。也许你可以运行它。
关于c++ - 需要帮助调试互斥锁死锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9604638/