in RH Linux, every pthread is mapping to a pid, which can be monitored in tools such as htop. but how can i get the pid of a thread? getpid() just return the pid of the main thread.
There are two thread values that get confused. pthread_self() will return the POSIX thread id; gettid() will return the OS thread id. The latter is linux specific and not guaranteed to be portable but probably what you are really looking for.
EDIT As PlasmaHH notes, gettid()
is called via syscall()
. From the syscall()
man page:
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
int
main(int argc, char *argv[])
{
pid_t tid;
tid = syscall(SYS_gettid);
}
pthread_self();
Can be called to return the ID of the calling thread.
Also PID is process Id, A thread has thread Id not PID. All threads running in the same process will have the same PID.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With