Ravi Chandra Ravi Chandra - 1 year ago 760
C Question

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

In linux c program, how to print thread id of a thread created by pthread library?

for ex: we can get pid of a process by


Answer Source

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.


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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download