The thread id returned by pthread_self () does not match the kernel thread id returned by the gettid (2) call

This quote is from the pthread_self () man page .

So, on what basis should I decide whether to use pthread_selfor gettidto determine which thread is performing the function?

Both are not tolerated.
Why are there two different functions for getting a stream identifier?

+4
source share
3 answers

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

pthread_self(), . gettid() , Linux. , gettid() ( srand()).

.

. gettid() Linux . pthread_self() , .

, .

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

, pthread_self() .

pthread_t my_tid; //filled elsewhere

pthread_t tid = pthread_self();

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

.

, , , .

?

. (pthread_self() (pthreads), (gettid() - . /syscall, , gettid(). gettid() .

+8

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

gettid() (thread-id) ( ) pthread ( Linux).

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

.

(or inside a program with multiple processes,

.

inside a process, different thread has different thread id.

.

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

.

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

, .

gettid() Linux , , , pthread - ( ) , getpid() ( ) pthread-id, pthread_self().

+4

( ).

, .

pid_t gettid(void);
pthread_t pthread_self(void);

pid_t, pthread_t. , thread, , software entities. a thread id , . , pthread_t , pthread, pid_t , ( Linux, pid_t).

, . pthread_t , pthread_t pid_t , pid_t - , .

. pid_t ==, pthread_t , pthread_equal.

/ , pthread , . pthread , thread id pthread_t. thread entity, (, Linux pid_t, Windows - DWORD).

So, although the underlying implementation may vary from OS to OS, code written against abstraction pthreadremains portable across all OSs (as long as you are limited to abstraction pthread).

+2
source

Source: https://habr.com/ru/post/1620866/


All Articles