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