2

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?

chub500
  • 712
  • 6
  • 18
  • Does this answer your question? [Why and in what sense is pthread\_t an opaque type?](https://stackoverflow.com/questions/33285562/why-and-in-what-sense-is-pthread-t-an-opaque-type) – Dan Fego May 08 '20 at 16:38
  • 3
    This is something that the glibc team has been arguing about internally for years. If you could extract the tid from a pthread_t you could do things with it that break the library's assumptions. On the other hand there are several valid use cases. So far no consensus and therefore no action. – zwol May 08 '20 at 16:40
  • I don't think so... I'm asking about `tid`s? As far as I understand pthread_t id's are a purely userspace contrivance for internal datastructures. – chub500 May 08 '20 at 16:41
  • I'm assuming the 'assumptions' you're referring to are some break from a 1-1 relationship between pthread_t and tid. Perhaps you can hack a pthread to have multiple stacks somehow (multiple tids)? I don't know how the other way around would be possible - 1 pthread_t mapping to 2 tids. – chub500 May 08 '20 at 16:50
  • @chub500 It's more complicated than that. I don't remember what the problem was well enough to explain and I don't have time today to dig up any of the threads on the mailing list about it. Sorry. – zwol May 08 '20 at 17:41
  • 1
    Well thank you for the pointer above anyway! A quick google lead me to this thread (no pun intended): https://sourceware.org/bugzilla/show_bug.cgi?id=6399 - incredibly informative. – chub500 May 08 '20 at 17:57

0 Answers0