Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

That quote is from the man page of pthread_self().

So, on what basis should I decide whether I should use pthread_self or gettid to determine which thread is running the function?

Both are non portable.
Why are there two different functions to get the thread ID?

like image 954
Aquarius_Girl Avatar asked Dec 19 '15 11:12

Aquarius_Girl


People also ask

What does pthread_self return?

The pthread_self() function returns the ID of the calling thread. This is the same value that is returned in *thread in the pthread_create(3) call that created this thread.

Is pthread_self a system call?

As you can see from the link above, pthread_self() returns THREAD_SELF, which is defined as a simple assembly movl instruction. No system calls there. Of course, this is implementation defined. The link above refers to glibc library.

How do I get my Pthread thread ID?

Syntax :- pthread_t pthread_self(void); The pthread_self() function returns the ID of the thread in which it is invoked.

How do I find the thread ID of a process in Linux?

Identifying the thread On Unix® and Linux® systems, you can use the top command: $ top -n 1 -H -p [pid]replacing [pid] with the process ID of the affected process. On Solaris®, you can use the prstat command: $ prstat -L -p [pid]replacing [pid] with the process ID of the affected process.


2 Answers

So, on what basis should I decide whether I should use pthread_self or gettid to determine which thread is running the function?

You should always use pthread_self() whenever you want to identify a thread within your application. gettid() can be used for certain purposes and if you know it's Linux. For example, gettid() can be used to get seed for a thread specific seed (used in srand()).

Both are non portable.

This is not entirely true. gettid() is not portable as its a Linux specific function. But pthread_self() is portable as long as you don't make any assumptions about its representation.

For example, the following is not portable.

printf("Thread ID is: %ld", (long) pthread_self());

as there's no guarantee that whatever pthread_self() is going to be an integer of some sort. But

pthread_t my_tid; //filled elsewhere

pthread_t tid = pthread_self();

if( pthread_equal(my_tid, tid) ) {
   /* do stuff */
}

is fully portable.

The former is not portable because it assumes that thread id is an integer whereas the latter is not.

Why are there two different functions to get the thread ID?

They are not two different ways to get the same value. One (pthread_self() is provided by the thread library (pthreads) while the other (gettid()is an OS-specific function. A different OS may provide a different interface/syscall to get thread ID similar to gettid(). So you can't rely on gettid() in a portable application.

like image 64
P.P Avatar answered Nov 15 '22 08:11

P.P


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().

like image 21
Abhijeet Kumar Srivastava Avatar answered Nov 15 '22 06:11

Abhijeet Kumar Srivastava