我在下面的代码中有一个互斥锁死锁:
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是类成员,不会在代码中的任何其他位置使用。在某些情况下,我可以看到一个线程试图锁定已经锁定的互斥锁。
有实时线程和非实时线程锁定互斥锁。我知道可能会有优先级反转,但这怎么会导致死锁呢?
发布于 2012-03-08 00:00:04
你的代码在我看来完全没问题。您确定在其他地方没有使用_Mutex吗?
Valgrind工具集包括Helgrind,这是一个可以帮助您解除死锁的pthread调试器。也许你可以运行它。
发布于 2012-03-08 00:15:31
您确定是死锁还是某个线程被其他优先级较高的线程长时间阻塞?
你有没有研究过使用
pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT)为了避免优先级反转问题?
https://stackoverflow.com/questions/9604638
复制相似问题