3

according to my understanding, the TID(thread id) returned by gettid() is unique inside a process(or inside a program with multiple processes, while each process may have multiple threads), namely, inside a process, different thread has different thread id.

the TID returned by pthread_self() is unique across processes, namely, on the same machine, different thread has different TID on the same machine at the same time.

Is my understanding correct or not?

besides, how can a thread get the TID(corresponds to gettid()) of its derived thread? thanks!

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
misteryes
  • 2,167
  • 4
  • 32
  • 58
  • 2
    `gettid()` returns something completely different then `pthread_self()`, although both identify the same thread. – alk May 17 '13 at 13:35
  • 2
    [**Whas is the difference between pthread_self() and gettid()? Which one should I use?**](http://stackoverflow.com/questions/6372102/whas-is-the-difference-between-pthread-self-and-gettid-which-one-should-i-u) – Grijesh Chauhan May 17 '13 at 13:37
  • @GrijeshChauhan: Depends on what you want to achieve... ;-) – alk May 17 '13 at 13:39
  • I know this, just I want to know more details, which are in my questions – misteryes May 17 '13 at 13:42
  • @GrijeshChauhan - no answer was accepted, so its not clear the correct answer has been provided in http://stackoverflow.com/questions/6372102/what-is-the-difference-between-pthread-self-and-gettid-which-one-should-i-u. – jww Dec 03 '13 at 22:56

3 Answers3

5

To put a bit of implementation context to this question, on Linux:

  1. gettid() returns a value corresponding to a thread's entry in the kernel process scheduler (very similar to what getpid() is perceived to be). This is something scheduling priorities and affinities can be set upon.

  2. pthread_self() returns a pointer to the vicinity of TLS (thread local storage) slot of the calling thread in the process memory map. Obviously, by definition of what TLS is, this value is unique within a given process (and can be mapped to kernel TID and back), but it can not possibly be unique system wide, because every process has the same virtual address range.

oakad
  • 6,945
  • 1
  • 22
  • 31
  • Note that on Linux, the gettid() is the SAME value reported by the unix tools 'top', 'htop', 'pidstat -t' and 'ps -ef'. This helps developers to correlate a troublesome process with a specific thread. The 'pidstat -t' tool will help identify excessive thread context switching. – J Jorgenson Oct 02 '14 at 15:25
3

In my observations pthread_self() is NOT unique within a process. It is unique at any given moment in time. But when threads terminate and other threads are started while the process is running, they may reuse pthread_self() values already used before by other threads.

  • 1
    "... observations `pthread_self` is NOT unique within a process" - Correct. Its documented at [pthread_self(3)](http://linux.die.net/man/3/pthread_self): "A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated". – jww Dec 03 '13 at 23:01
1

pthread_self() returns the process-wide unique pthread-id.

gettid() returns the (pthread implementation specific) system-wide unique thread-id (on Linux).


the TID(thread id) returned by gettid() is unique inside a process

Yes.

(or inside a program with multiple processes,

Yes.

inside a process, different thread has different thread id.

Yes.

the TID returned by pthread_self() is unique across processes,

No.

different thread has different TID on the same machine at the same time.

Yes in the same process, No across the whole machine.


As gettid() is Linux specific and therefore not portable, the only way to system widely identify a pthread is to use its (system wide unique) parent process id as returned by getpid() along with its (process-wide unique) pthread-id as returned by pthread_self().

alk
  • 69,737
  • 10
  • 105
  • 255
  • I know this, just I want to know more details, which are in my questions – misteryes May 17 '13 at 13:39
  • @misteryes: What is not in my answer, what you want to know? – alk May 17 '13 at 13:40
  • Is my understanding correct or not?how can a thread get the TID(corresponds to gettid()) of its derived thread? – misteryes May 17 '13 at 13:43
  • it seems to me that `gettid()` will occupy many process id. if a thread is allocated with the value of `gettid()`, that value can't be used by other processes during the thread's active time. right? – misteryes May 17 '13 at 14:17
  • sorry, a last question. Since the structure of pthread_t is opaque, if pthread_t is not an atom type, e.g, it is a strucutre, then the assignment operator is wrong? it should use a pointer instead? `pthread_t pthread_self(void);` – misteryes May 17 '13 at 14:59
  • @misteryes: I'm not sure if I understand what you are asking. `pthread_t pthread = pthread_self();` is correct and portable. – alk May 17 '13 at 15:03
  • i.e, pthread_t is a structure, then `pthread_self()` returns a struct variable? I think structure assignment is not allowed. – misteryes May 17 '13 at 15:08
  • @misteryes: On x86 Linux `pthread_t` is implemented as `unsigned long int`. However this is **not** exposed, as this is implementation specific, will say `pthread_t` may be defined differently on different platforms. On MVS it's a pointer, for example ... - if I remember correctly. – alk May 17 '13 at 15:16
  • that's what I said, since it is an atom type, so there is no problem in linux. but if it is designed likt `typedef struct{long id; int flag;} pthread_t;` then it is a problem. – misteryes May 17 '13 at 15:19
  • @misteryes: Returning structures is allowed, as well as passing them as parameter and assigning them. – alk May 17 '13 at 15:23