In Linux, the tid is retrieved with a syscall: gettid()
.
The pthread object stores the tid in struct pthread:
struct pthread {
...
/* This descriptor's link on the `stack_used' or `__stack_user' list. */
list_t list;
/* Thread ID - which is also a 'is this thread descriptor (and
therefore stack) used' flag. */
pid_t tid;
/* Ununsed. */
pid_t pid_ununsed;
...
};
I believe this structure is stored at the base of the stack? Anyway, every pthread has access to it. For instance, in the pthread_create implementation, it grabs the struct pthread:
struct pthread *self = THREAD_SELF;
So my question is: why is there no pthread_gettid_np()
call? Perhaps gettid()
is so fast somehow it's negligible? Or maybe the call exists and I just can't find it anywhere?