A thread has a mutex of type pthread_mutex_t
locked for itself. Another thread wants to know the thread id of the thread holding this locked mutex.
There are two types of thread ids as I understand. The POSIX/pthread thread id, returned by pthread_self()
, and the linux thread id returned by the system call gettid()
. These two are independent and have no relation, AFAIK (please correct me If I am wrong).
There is a field in the structure pthread_mutex_t
, int __owner
which stores the thread id of the thread that current holds the lock. This field can be accessed by,
pthread_mutex_t mutex;
int tid;
tid = mutex.__data.__owner;
As described here - Is it possible to determine the thread holding a mutex?.
This __owner
field has the linux system thread id (as would be returned by gettid()
) and not the POSIX/pthread thread id (as would be returned by pthread_self()
) .
I want to compare whether the current scheduled thread is owning the mutex or not. So, I should be comparing pthread_self()
with the __owner
value.
I could use gettid()
instead of pthread_self()
, but I am restricted to use pthread_self()
only. (some portability features).
Is there any way to correctly determine the thread id of the locked mutex which would return pthread_t
and not the system thread id?
I will appreciate any help, Thanks!
Regards,
Yusuf Husainy.