Astroplex
Astroplex

Reputation: 13

I have questined about get tid on Linux System and result of getpid

pid : 2394 tid : 2399

pid : 2394 tid : 2398

pid : 2394 tid : 2397

pid : 2394 tid : 2395

*this is using getpid() __NR_gettid*

pid :2529 tid : 1811777280

pid :2529 tid : 1820169984

pid :2529 tid : 1828562688

pid :2529 tid : 1836955392

pid :2529 tid : 1845348096

*this is using pthread_self()*

I am using Fedora17. I was wondering why tid result differ between getpid() and pthread_self()?

which one is right? my prof. said pthread uses posix not linux system.

and in multi thread condition pid is same? am i got right result?

Upvotes: 0

Views: 791

Answers (1)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57690

gettid returns thread ID and pthread_self returns POSIX thread ID. Both differs.

This is the default behavior. See the linux man page of pthread_self(). These 3 clauses will clarify it.

Thread identifiers should be considered opaque: any attempt to use a thread ID other than in pthreads calls is nonportable and can lead to unspecified results.

Thread IDs are only guaranteed to be unique within a process. A thread ID may be reused after a terminated thread has been joined, or a detached thread has terminated.

The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to gettid(2).

Also this from gettid man page

The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by pthread_self(3))

Upvotes: 3

Related Questions