#include <stdio.h>
#include <sys/syscall.h>
#include <unistd.h>
//
//#include <sys/types.h>
int main()
{
printf("hello,tid is %ld\n",(long int)syscall(SYS_gettid));
}
~
http://blog.csdn.net/tianyue168/article/details/7403693
http://blog.csdn.net/delphiwcdj/article/details/8476547
http://zhwen.org/xlog/?p=622
这几天写程序老是使用thread来做,所以自己封装了一个threadbase的类来做简单的测试使用,但是在写的过程种发现又两个获取线程id的函数:pthread_self和gettid,那这两个函数有什么区别呢?
看gettid的man,这样写道:
gettid() returns the caller’s thread ID (TID). In a single-threaded process, the thread ID is equal to the process ID
(PID, as returned by getpid(2)). In a multithreaded process, all threads have the same PID, but each one has a unique
TID. For further details, see the discussion of CLONE_THREAD in clone(2).
The thread ID returned by this call is not the same thing as a POSIX thread ID (i.e., the opaque value returned by
pthread_self(3)).
而pthread_self的man这样写:
Thread IDs are only guaranteed to be unique within a process. A thread ID may be reused after a terminated thread has
been joined, or a detached thread has terminated.
The thread ID returned by pthread_self() is not the same thing as the kernel thread ID returned by a call to get‐
tid(2).
现在可以大体知道pthread_self是为了区分同一进程种不同的线程,而gettid获取的线程id和pid是有关系的。