How to get thread id of a pthread in linux c program?

CLinuxPthreads

C Problem Overview


In a Linux C program, how do I print the thread id of a thread created by the pthread library? For example like how we can get pid of a process by getpid().

C Solutions


Solution 1 - C

What? The person asked for Linux specific, and the equivalent of getpid(). Not BSD or Apple. The answer is gettid() and returns an integral type. You will have to call it using syscall(), like this:

#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h>

 ....

 pid_t x = syscall(__NR_gettid);

While this may not be portable to non-linux systems, the threadid is directly comparable and very fast to acquire. It can be printed (such as for LOGs) like a normal integer.

Solution 2 - C

pthread_self() function will give the thread id of 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 must use pthread_getthreadid_np() to return an integral identifier for the thread.

NOTE:

pthread_id_np_t   tid;
tid = pthread_getthreadid_np();

is 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);

Solution 3 - C

As noted in other answers, pthreads does not define a platform-independent way to retrieve an integral thread ID.

On Linux systems, you can get thread ID thus:

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

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

However, if the reason you think you need a thread ID is to know whether you're running on the same or different thread to another thread you control, you might find some utility 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're on the main thread, there are additional ways, documented in answers to this question https://stackoverflow.com/questions/4867839/how-can-i-tell-if-pthread-self-is-the-main-first-thread-in-the-process.

Solution 4 - C

pid_t tid = syscall(SYS_gettid);

Linux provides such system call to allow you get id of a thread.

Solution 5 - C

You can use pthread_self()

The parent gets to know the thread id after the pthread_create() is executed sucessfully, but while executing the thread if we want to access the thread id we have to use the function pthread_self().

Solution 6 - C

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

 printf("before calling pthread_create getpid: %d getpthread_self: %lu tid:%lu\n",getpid(), pthread_self(), syscall(SYS_gettid));

Solution 7 - C

I think not only is the question not clear but most people also are not cognizant of the difference. Examine the following saying,

> POSIX thread IDs are not the same as the thread IDs returned by the > Linux specific gettid() system call. POSIX thread IDs are assigned > and maintained by the threading implementation. The thread ID returned > by gettid() is a number (similar to a process ID) that is assigned by > the kernel. Although each POSIX thread has a unique kernel thread ID > in the Linux NPTL threading implementation, an application generally > doesn’t need to know about the kernel IDs (and won’t be portable if it > depends on knowing them).

> Excerpted from: The Linux Programming Interface: A Linux and UNIX System Programming Handbook, Michael Kerrisk

IMHO, there is only one portable way that pass a structure in which define a variable holding numbers in an ascending manner e.g. 1,2,3... to per thread. By doing this, threads' id can be kept track. Nonetheless, 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");

Solution 8 - C

pthread_getthreadid_np wasn't on my Mac os x. pthread_t is an opaque type. Don't beat your head over it. Just assign it to void* and call it good. If you need to printf use %p.

Solution 9 - C

There is also another way of getting thread id. While 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 a thread id (that is an unsigned long int defined in bits/pthreadtypes.h). Also, the last argument void *arg is the argument that is passed to void * (*start_routine) function to be threaded.

You can create a structure to pass multiple arguments and send a pointer to a 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
}

Solution 10 - C

For different OS there is different answer. I find a helper here.

You can try this:

#include <unistd.h>
#include <sys/syscall.h>

int get_thread_id() {
#if defined(__linux__)
    return syscall(SYS_gettid);
#elif defined(__FreeBSD__)
    long tid;
    thr_self(&tid);
    return (int)tid;
#elif defined(__NetBSD__)
    return _lwp_self();
#elif defined(__OpenBSD__)
    return getthrid();
#else
    return getpid();
#endif
}

Solution 11 - C

Platform-independent way (starting from c++11) is:

#include <thread>

std::this_thread::get_id();

Solution 12 - C

You can also write in this manner and it does the same. For eg:

for(int i=0;i < total; i++)
{
	pthread_join(pth[i],NULL);
	cout << "SUM of thread id " << pth[i] << " is " << args[i].sum << endl;
}

This program sets up an array of pthread_t and calculate sum on each. So it is printing the sum of each thread with thread id.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRavi ChandraView Question on Stackoverflow
Solution 1 - CEvan LangloisView Answer on Stackoverflow
Solution 2 - CAbhitesh khatriView Answer on Stackoverflow
Solution 3 - CbleaterView Answer on Stackoverflow
Solution 4 - CWeiqi GuView Answer on Stackoverflow
Solution 5 - CJeffView Answer on Stackoverflow
Solution 6 - CnandanView Answer on Stackoverflow
Solution 7 - CsnrView Answer on Stackoverflow
Solution 8 - Cuser2074102View Answer on Stackoverflow
Solution 9 - Cemre canView Answer on Stackoverflow
Solution 10 - CJianyong WuView Answer on Stackoverflow
Solution 11 - COleg OlegView Answer on Stackoverflow
Solution 12 - CPranav KumarView Answer on Stackoverflow