Is it possible to tell which thread has a pthread mutex lock at a given moment?
Is it possible to tell which thread has a pthread mutex lock at a given moment?
It might be, if you told us which implementation you're working with.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
There's nothing in standard posix for doing that (that I could find). But your implementation of pthreads could have non-stadard "features" for doing such things.
You could create your own mutex lock "object wrapper" and save off the owning thread yourself using pthread_self().
gg
Just curious to know, how many thread are there in your implementation and how many of them uses the Critical part.
ssharish
Last edited by ssharish2005; 09-17-2007 at 12:22 PM.
If your implementation of pthreads supports recursive mutexes, then there is probably a way to get it. A recursive mutex can be repeatedly locked by the thread which holds it -- this implies that the identity of this thread is stored somewhere. Whether you can actually gain access to this identity, I don't know. The answer is undoubtedly going to involve poking in the pthreads internals.
Otherwise, just invent your own mutex structure to wrap the mutex in question, and keep the identity of the locking thread up to date along with the mutex.