Need help debugging deadlocked mutex

I have a dead mutex lock in the following code:

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

is a member of the class and is not used anywhere else in your code. At some point, I see that a thread is stuck trying to lock an already locked mutex.

There are both real-time and non-real-time threads that block the mutex. I understand that there might be a preemptive inversion, but how can this lead to blocking?

+3


source to share


2 answers


Your code seems fine to me. Are you sure _Mutex is not used anywhere else?



The Valgrind toolbox includes Helgrind , a pthread debugger that can help you sort out dead ends. Perhaps you can run it.

+2


source


Are you sure there is a lock or just a thread that has been blocked for a long time by other higher priority threads?

Have you studied using



pthread_mutexattr_setprotocol (&attr, PTHREAD_PRIO_INHERIT)

      

to avoid the priority inversion problem?

0


source







All Articles