gettid 和pthread_self的区别
来源:互联网 发布:格力空调代码含义 知乎 编辑:程序博客网 时间:2024/05/16 19:29
Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
linux多线程环境下gettid() pthread_self() 两个函数都获得线程ID,可它们的返回值不一样。
linux使用进程模拟线程,gettid 函数返回实际的进程ID(内核中的线程的ID).
pthread_self 函数返回 pthread_create创建线程时的ID(POSIX thread ID).
为什么有两个thread ID:
线程库实际上由两部分组成:内核的线程支持+用户态的库支持(glibc)。Linux在早期内核不支持线程的时候,glibc就在库中(用户态)以线程(就是用户态线程)的方式支持多线程了。POSIX thread只对用户编程的调用接口作了要求,而对内核接口没有要求。linux上的线程实现就是在内核支持的基础上,以POSIX thread的方式对外封装了接口,所以才会有两个ID的问题。
在glibc中,pthread_self()返回的是THREAD_SELF,这是一个宏。其定义如下
({ struct pthread *__self; \
asm ("movl %%gs:%c1,%0" : "=r" (__self) \
: "i" (offsetof (struct pthread, header.self))); \
__self;})
这段代码返回了当前线程的descriptor(即struct pthread结构),pthread_self()得到的就是这个descriptor的地址, 也就是unsigned long int类型的pthread_t。知道了这一点就好办了,找到thread descriptor的定义:
{
...
pid_t tid;
...
}
接下来知道怎么做了吗?算好长度n,构造一个假的pthread结构。
{
void *nothing[n];
pid_t tid;
};
用(struct pthread_fake *) pthread_self()->tid得到线程id了。
The man page for gettid says:
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)).
- gettid 和pthread_self的区别
- gettid 和 pthread_self 的区别
- gettid 和pthread_self的区别
- gettid 和pthread_self的区别
- gettid 和pthread_self的区别
- gettid 和pthread_self的区别
- gettid和pthread_self区别
- gettid和pthread_self区别
- 获取线程标识函数gettid和pthread_self的区别
- 线程pthread_self与gettid 的区别
- linux的pthread_self与gettid的返回值和开销的区别
- linux的pthread_self与gettid的返回值和开销的区别
- thread 的gettid和pthread_self區别(TODO)
- POSIX定时器timer_create()以及线程中的gettid() 和pthread_self()
- gettid
- 笔记九:线程间的通信(pthread_create()和pthread_self())
- pthread_detach(pthread_self())的作用
- thread_detach(threadid)和pthread_detach(pthread_self())
- Swift学习(持续更新)
- win10下详细配置nodejs+express4开发环境
- VS2015 ATL 在XP 系统下无法注册
- Android Context完全解析,你所不知道的Context的各种细节
- Javascript中Event属性大全
- gettid 和pthread_self的区别
- VGA接口时序练习之图像动态显示
- JSON中JObject和JArray,JValue序列化(Linq)
- 如何打造一个可以无限注册账号的手机
- linux编译boost
- 互联网推送服务原理:长连接+心跳机制(MQTT协议)
- Spring学习笔记(十三):jsp、freemarker、velocity的区别
- Objective-c语言_基本数据类型封装和拆包
- 解决Xcode 9.2系统真机测试时出现 could not find developer disk image问题