获得线程LWP

 #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是有关系的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值