Linux中,每个线程有一个tid,类型long,由sys_gettid()取得。

Linux内核中并没有实现线程,而是由glibc线程库实现的POSIX线程。每个线程也有一个id,类型 pthread_t(unsigned long int),由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。Linux中的POSIX线程库实现的线程在内核中看来也是一个轻量级进程(LWP),但是该进程与主进程(启动线程的进程)共享一些资源,比如代码段,数据段等。

1. gettid()是linux内核实现的函数,在内核看来任何线程也是一个轻量级进程,从下面内核实现的sys_gettid看来,gettid()返回的是内核管理的轻量级进程的进程id。

/* Thread ID - the internal kernel "pid" */
asmlinkage long sys_gettid(void)
{
return current->pid;
}

2. pthread_self()在glibc中X86_64平台的实现如下:

pthread_t __pthread_self (void)
{
return (pthread_t) THREAD_SELF;
}

/* Return the thread descriptor for the current thread.

The contained asm must *not* be marked volatile since otherwise
assignments like
pthread_descr self = thread_self();
do not get optimized away. */
# define THREAD_SELF \
({ struct pthread *__self; \
asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
: "i" (offsetof (struct pthread, header.self))); \
__self;})

从上面代码我们可以知道__pthread_self 得到实际上是线程描述符pthread 指针地址。

从上面我们可以得知,gettid()是内核给线程(轻量级进程)分配的进程id,全局(所有进程中)唯一;pthread_self()是在用户态实现的,获取的id实际上是主线程分配给子线程的线程描述符的地址而已,只是在当前进程空间中是唯一的。

gettid()和pthread_self()的区别的更多相关文章

  1. gettid 和pthread_self的区别

    转: Linux中,每个进程有一个pid,类型pid_t,由getpid()取得.Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库 ...

  2. gettid和pthread_self区别

    http://blog.csdn.net/rsyp2008/article/details/45150621 1 线程ID获取方法 Linux下获取线程有两种方法: 1)gettid或者类似getti ...

  3. getpid 与 gettid 与 pthread_self

    获取进程的PID(process ID) #include <unistd.h> pid_t getpid(void); 获取线程的TID(thread ID) 1)gettid或者类似g ...

  4. pthread_detach

    http://blog.csdn.net/scanery/article/details/7241890   感谢作者! 近来发现 在线程函数第一行调用 pthread_detach(pthread_ ...

  5. pthread_join与pthread_detach细节问题

    http://www.360doc.com/content/13/0106/09/9171956_258497083.shtml pthread_t    pthr; pthread_create(& ...

  6. 【Linux开发】彻底释放Linux线程的资源

    Linux系统中程序的线程资源是有限的,表现为对于一个程序其能同时运行的线程数是有限的.而默认的条件下,一个线程结束后,其对应的资源不会被释放,于是,如果在一个程序中,反复建立线程,而线程又默认的退出 ...

  7. linux的pthread_self与gettid的返回值和开销的区别

    linux的pthread_self与gettid的返回值和开销的区别 linux的pthread_self与gettid的返回值和开销的区别 分类: 一些思考 2012-05-18 12:25 17 ...

  8. Linux下获取线程TID的方法——gettid()

    (转载)http://blog.csdn.net/delphiwcdj/article/details/8476547 如何获取进程的PID(process ID)? 可以使用: #include & ...

  9. c#与java的区别

    经常有人问这种问题,用了些时间java之后,发现这俩玩意除了一小部分壳子长的还有能稍微凑合上,基本上没什么相似之处,可以说也就是马甲层面上的相似吧,还是比较短的马甲... 一般C#多用于业务系统的开发 ...

随机推荐

  1. PHP向MySql中插入数据

    <!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Prope ...

  2. 《Linear Algebra and Its Applications》-chaper1-线性方程组- 线性变换

    两个定理非常的简单显然,似乎是在证明矩阵代数中的基本运算律.但是它为后面用“线性变换”理解矩阵-向量积Ax奠定了理论基础. 结合之前我们讨论过的矩阵和向量的积Ax的性质,下面我们就可以引入线性变换了. ...

  3. 【ACM/ICPC2013】线段树题目集合(一)

    前言:前一段时间在网上找了一个线段树题目列表,我顺着做了一些,今天我把做过的整理一下.感觉自己对线段树了解的还不是很深,自己的算法能力还要加强.光练代码能力还是不够的,要多思考.向队友学习,向大牛学习 ...

  4. etc下

    用户账号与密码参数:  /etc/passwd  .  /etc/shadow 用户组相关方面的条件:     /etc/group   .   /etc/gshadow 用户个人文件数据:   /h ...

  5. 【转】shell 教程——04 什么时候使用Shell

    因为Shell似乎是各UNIX系统之间通用的功能,并且经过了POSIX的标准化.因此,Shell脚本只要“用心写”一次,即可应用到很多系统上.因此,之所以要使用Shell脚本是基于: 简单性:Shel ...

  6. Spring基于注解的Cache支持

    Spring为我们提供了几个注解来支持Spring Cache.其核心主要是@Cacheable和@CacheEvict.使用@Cacheable标记的方法在执行后Spring Cache将缓存其返回 ...

  7. Java 线程第三版 第一章Thread导论、 第二章Thread的创建与管理读书笔记

    第一章 Thread导论 为何要用Thread ? 非堵塞I/O      I/O多路技术      轮询(polling)      信号 警告(Alarm)和定时器(Timer) 独立的任务(Ta ...

  8. xcode6下使用autolayout+sizeclass实践

    历史车轮滚滚向前,将autolayout配合sizeclass做布局的方式推上了主流,虽然有点晚,但最终还是进行了一次完整的实践,特此记录一下: 因为网上已经有很多博客介绍了autolayout配合s ...

  9. MYSQL 学习笔记1 -----mysqladmin -uroot -p status|extended-status

    root@server1 ~]# mysqladmin -uroot -p status -i -r extended-status|grep Handler_commit Enter passwor ...

  10. SecureCRT使用教程

    Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT 支持标签 ...