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

后端 未结 11 789
忘了有多久
忘了有多久 2020-12-02 07:05

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

11条回答
  •  死守一世寂寞
    2020-12-02 07:50

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

提交回复
热议问题