Is it possible to determine the thread holding a mutex?

0 votes
asked Aug 14, 2010 by terry

Firstly, I use pthread library to write multithreading C program. Threads always hung by their waited mutexs. When I use the strace utility to find a thread is in FUTEX_WAIT status, I want to know which thread hold that mutex at the time. But I don't know how could I make it. Are there any utilities could do that?

Someone told me Java virtual machine support this, so I want to know whether Linux support this feature.

4 Answers

0 votes
answered Aug 14, 2010 by duck

I don't know of any such facility so I don't think you will get off that easily - and it probably wouldn't be as informative as you think in helping to debug your program. As low tech as it might seem, logging is your friend in debugging these things. Start collecting your own little logging functions. They don't have to be fancy, they just have to get the job done while debugging.

Sorry for the C++ but something like:

void logit(const bool aquired, const char* lockname, const int linenum)

    if (! aquired)
        logfile << pthread_self() << " tries lock " << lockname << " at " << linenum << endl;
        logfile << pthread_self() << " has lock "   << lockname << " at " << linenum << endl;


void someTask()
    logit(false, "some_mutex", __LINE__);


    logit(true, "some_mutex", __LINE__);

    // do stuff ...


Logging isn't a perfect solution but nothing is. It usually gets you what you need to know.

0 votes
answered Aug 16, 2010 by caf

You can use knowledge of the mutex internals to do this. Ordinarily this wouldn't be a very good idea, but it's fine for debugging.

Under Linux with the NPTL implementation of pthreads (which is any modern glibc), you can examine the __data.__owner member of the pthread_mutex_t structure to find out the thread that currently has it locked. This is how to do it after attaching to the process with gdb:

(gdb) thread 2
[Switching to thread 2 (Thread 0xb6d94b90 (LWP 22026))]#0  0xb771f424 in __kernel_vsyscall ()
(gdb) bt
#0  0xb771f424 in __kernel_vsyscall ()
#1  0xb76fec99 in __lll_lock_wait () from /lib/i686/cmov/
#2  0xb76fa0c4 in _L_lock_89 () from /lib/i686/cmov/
#3  0xb76f99f2 in pthread_mutex_lock () from /lib/i686/cmov/
#4  0x080484a6 in thread (x=0x0) at mutex_owner.c:8
#5  0xb76f84c0 in start_thread () from /lib/i686/cmov/
#6  0xb767784e in clone () from /lib/i686/cmov/
(gdb) up 4
#4  0x080484a6 in thread (x=0x0) at mutex_owner.c:8
8               pthread_mutex_lock(&mutex);
(gdb) print mutex.__data.__owner
$1 = 22025

(I switch to the hung thread; do a backtrace to find the pthread_mutex_lock() it's stuck on; change stack frames to find out the name of the mutex that it's trying to lock; then print the owner of that mutex). This tells me that the thread with thread id 22025 is the culprit.

0 votes
answered Aug 17, 2012 by yusuf-khan

Normally libc/platforms calls are abstracted by OS abstraction layer. The mutex dead locks can be tracked using a owner variable and pthread_mutex_timedlock. Whenever the thread locks it should update the variable with own tid(gettid() and can also have another variable for pthread id storage) . So when the other threads blocks and timed out on pthread_mutex_timedlock it can print the value of owner tid and pthread_id. this way you can easily find out the owner thread. please find the code snippet below, note that all the error conditions are not handled

pid_t ownerTid;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

class TimedMutex {
           struct timespec abs_time;

               clock_gettime(CLOCK_MONOTONIC, &abs_time);
               abs_time.tv_sec += 10;
               if(pthread_mutex_timedlock(&mutex,&abs_time) == ETIMEDOUT)
                   log("Lock held by thread=%d for more than 10 secs",ownerTid);
               ownerTid = gettid();



There are other ways to find out dead locks, maybe this link might help

0 votes
answered Aug 9, 2015 by jossy-sebastian

Please read below link, This has a generic solution for finding the lock owner. It works even if lock in side a library and you don't have the source code.

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter