如何让Linux下C程序中的pthread的线程ID? [英] how to get thread id of a pthread in linux c program?
问题描述
在Linux下C程序中,如何打印由pthread库创建线程的线程ID?结果
为前:我们可以通过 GETPID得到一个进程的PID()
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()
推荐答案
pthread_self()
功能将给当前线程的线程ID。
pthread_self()
function will give the thread id of current thread.
pthread_t pthread_self(void);
的 pthread_self()
函数返回调用线程的Pthread的句柄。该pthread_self()函数不返回调用线程的积分线程。您必须使用 pthread_getthreadid_np()
返回一个整数标识符主题。
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);
这篇关于如何让Linux下C程序中的pthread的线程ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!