How to get pthread thread id in linux c program?

In linux c program, how to print the thread stream id created by pthread library?

for example: we can get the pid of the getpid() process

+64
c linux pthreads
Jan 13 '14 at 12:12
source share
10 answers
Function

pthread_self() will give the thread id of the current thread.

 pthread_t pthread_self(void); 

The pthread_self() function returns the Pthread handle of the calling thread. The pthread_self () function does NOT return the integral thread of the calling thread. You should use pthread_getthreadid_np() to return the integral identifier of the thread.

Note:

 pthread_id_np_t tid; tid = pthread_getthreadid_np(); 

significantly faster than these calls, but provides the same behavior.

 pthread_id_np_t tid; pthread_t self; self = pthread_self(); pthread_getunique_np(&self, &tid); 
+64
Jan 18 '14 at 15:54
source share

What? The man asked for Linux and the equivalent of getpid (). Not BSD or Apple. Answer: gettid () and returns an integer type. You will need to call it using syscall (), for example:

 #include <sys/types.h> #include <sys/syscall.h> .... pid_t x = syscall(__NR_gettid); 

Although this may not be portable for systems other than linux, the data stream is directly comparable and can be implemented very quickly. It can be printed (for example, for LOG) as a normal integer.

+44
Aug 25 '15 at 18:19
source share

You can use pthread_self()

The parent will know the thread id after pthread_create() is successful, but when the thread is running, if we want to access the thread id, we must use the pthread_self() function.

+9
Jan 13 '14 at 12:15
source share

As noted in other answers, pthreads does not define a platform-independent way of obtaining an integral thread identifier.

On Linux systems, you can get the thread id like this:

 #include <sys/types.h> pid_t tid = gettid(); 

On many BSD-based platforms, this answer https://stackoverflow.com/a/212251/ ... gives a non-portable way.

However, if the reason you need a thread identifier is to know if you are working on the same thread with another thread that you control, you may find some usefulness in this approach

 static pthread_t threadA; // On thread A... threadA = pthread_self(); // On thread B... pthread_t threadB = pthread_self(); if (pthread_equal(threadA, threadB)) printf("Thread B is same as thread A.\n"); else printf("Thread B is NOT same as thread A.\n"); 

If you just need to know if you are in the main thread, there are additional ways documented in the answers to this question, how can I determine if pthread_self is the main (first) in the process? .

+8
Dec 01
source share
 pid_t tid = syscall(SYS_gettid); 

Linux provides such a system call so that you can get the thread id.

+7
Apr 09 '17 at 20:22
source share

This single line gives you pid, each stream and spid.

  printf("before calling pthread_create getpid: %d getpthread_self: %lu tid:%lu\n",getpid(), pthread_self(), syscall(SYS_gettid)); 
+3
Sep 18 '15 at 10:08
source share

pthread_getthreadid_np not on my Mac os x. pthread_t is an opaque type. Do not hit your head. Just assign it void* and name it well. If you need printf use %p .

+3
Feb 28 '16 at 15:13
source share

There is also another way to get the stream identifier. When creating threads with

int pthread_create(pthread_t * thread, const pthread_attr_t * attr, void * (*start_routine)(void *), void *arg);

function call; The first parameter pthread_t * thread is actually the pthread_t * thread identifier (which is an unsigned long int, defined in the /pthreadtypes.h bits). In addition, the last argument void *arg is the argument that is passed to the void * (*start_routine) for void * (*start_routine) .

You can create a structure to pass multiple arguments and send a pointer to the structure.

 typedef struct thread_info { pthread_t thread; //... } thread_info; //... tinfo = malloc(sizeof(thread_info) * NUMBER_OF_THREADS); //... pthread_create (&tinfo[i].thread, NULL, handler, (void*)&tinfo[i]); //... void *handler(void *targs) { thread_info *tinfo = targs; // here you get the thread id with tinfo->thread } 
+2
Feb 15 '18 at 7:20
source share

Platform independent method (starting with C ++ 11):

 #include <thread> std::this_thread::get_id(); 
+1
Sep 09 '18 at 12:41
source share

I think the question is not only unclear, but most people are not aware of the difference. Study the following statement:

POSIX thread IDs do not match thread IDs returned by the gettid() system call. POSIX thread identifiers are assigned and supported by thread implementations. The thread identifier returned by gettid() is a number (similar to the process identifier) ​​assigned by the kernel. Although each POSIX thread has a unique kernel thread identifier in the Linux NPTL streaming process implementation, an application usually does not need to know about kernel identifiers (and will not be portable if it depends on their knowledge).

Excerpt from: Linux Programming Interface: Linux and UNIX System Programming Guide, Michael Kerrisk

IMHO, there is only one portable way, which conveys a structure in which a variable containing numbers in ascending order, for example, 1,2,3... for each thread. By doing this, thread IDs can be tracked. However, the int pthread_equal(tid1, tid2) function should be used.

 if (pthread_equal(tid1, tid2)) printf("Thread 2 is same as thread 1.\n"); else printf("Thread 2 is NOT same as thread 1.\n"); 
0
Jan 26 '19 at 9:20
source share



All Articles