Search code examples
c++11g++deadlockvalgrind

helgrind does not detect recursive locking of std::mutex


I observed that helgrind won't detect a recursive lock on a non-recursive c++11 std::mutex. The problem is however detected when using pthread_mutex_lock.

Two simple testcases to demonstrate the problem:

// Test code: C++11 std::mutex
// helgrind does not detect recursive locking
void test_cpp11()
{
    std::mutex m;
    m.lock();
    m.lock();
}

// pthread-based test code
// helgrind does detect recursive locking
void test_pth()
{
    pthread_mutex_t m;
    pthread_mutex_init(&m, 0);
    pthread_mutex_lock(&m);
    pthread_mutex_lock(&m);
}

gdb shows that the same pthread library functions are being called:

#0  __lll_lock_wait () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:135
#1  0x00007ffff78c2657 in _L_lock_909 () from /lib/x86_64-linux-gnu/libpthread.so.0
#2  0x00007ffff78c2480 in __GI___pthread_mutex_lock (mutex=0x7fffffffe450) at ../nptl/pthread_mutex_lock.c:79
#3  0x00000000004008ad in test_pth() ()

#1  0x00007ffff78c2657 in _L_lock_909 () from /lib/x86_64-linux-gnu/libpthread.so.0
#2  0x00007ffff78c2480 in __GI___pthread_mutex_lock (mutex=0x7fffffffe450) at ../nptl/pthread_mutex_lock.c:79
#3  0x00000000004007f7 in __gthread_mutex_lock(pthread_mutex_t*) ()
#4  0x00000000004008ec in std::mutex::lock() ()
#5  0x0000000000400857 in test_cpp11() ()

This was observed with g++ 4.7.3, 4.8.2 and 4.9.0 on Ubuntu 14.04 64-bit.

Does anyone have an idea what might be the reason and what might be done to get helgrind to detect the recursive locking?


Solution

  • Not an answer to the original question but I think it's worth mentioning that one should always check the program with both helgrind and drd. The drd tool successfully detects the problem in both scenarios.