linux 线程创建 pthread_create函数 获取线程id
2017-11-30 22:06
405 查看
函数原型:
#include<pthread.h>
int pthread_create(pthread_t*thread,pthread_attr_t *attr,
void * (*start_routine)(void *arg), void *arg);
参数
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的地址。
最后一个参数是运行函数的参数。
返回值:
若成功则返回0,否则返回出错编号
代码:
#include <stdio.h>
#include <pthread.h>
#include <sys/syscall.h>
void* Func_pth1()
{
printf("child gettid = %u\n", syscall(SYS_gettid));
printf("child pthread_self= %u\n", (unsigned int)pthread_self());
//printf("child thread tid = %u\n", pthread_self());
}
int main()
{
int iRet = 0;
pthread_t pth1;
printf("main gettid = %u\n", syscall(SYS_gettid));
printf("main pthread_self = %u\n", (unsigned int)pthread_self());
iRet = pthread_create(&pth1, NULL, (void*)Func_pth1, NULL);
if(iRet)
{
printf("create pthread fail\n");
}
printf("---pthread_t = %u\n", (unsigned int)pth1);
pthread_join(pth1, NULL);
return 0;
}
运行结果:
main gettid = 5100
main pthread_self = 3078461120
---pthread_t = 3078458224
child gettid = 5101
child pthread_self= 3078458224
分析:
1 pthread_self()是POSIX的实现,它的返回值是pthread_t,pthread_t在linux中实际是无符号长整型,即unsigned long。gettid是系统调用,它的返回值是pid_t,在linux上是一个无符号整型。但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。
2 pthread_self是为了区分同一进程中不同的线程, 是由thread的实现来决定的。pthread_self返回的是同一个进程中各个线程之间的标识号,对于这个进程内是唯一的,而不同进程中,每个线程返回的pthread_self可能返回的是一样的。而gettid获取的线程id和pid是有关系的,因为在linux中线程其实也是一个进程(clone),所以它的线程ID也是pid_t类型。在一个进程中,主线程的线程id和进程id是一样的,该进程中其他的线程id则在linux系统内是唯一的,gettid是不可移植的。
3 Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid。
#include<pthread.h>
int pthread_create(pthread_t*thread,pthread_attr_t *attr,
void * (*start_routine)(void *arg), void *arg);
参数
第一个参数为指向线程标识符的指针。
第二个参数用来设置线程属性。
第三个参数是线程运行函数的地址。
最后一个参数是运行函数的参数。
返回值:
若成功则返回0,否则返回出错编号
代码:
#include <stdio.h>
#include <pthread.h>
#include <sys/syscall.h>
void* Func_pth1()
{
printf("child gettid = %u\n", syscall(SYS_gettid));
printf("child pthread_self= %u\n", (unsigned int)pthread_self());
//printf("child thread tid = %u\n", pthread_self());
}
int main()
{
int iRet = 0;
pthread_t pth1;
printf("main gettid = %u\n", syscall(SYS_gettid));
printf("main pthread_self = %u\n", (unsigned int)pthread_self());
iRet = pthread_create(&pth1, NULL, (void*)Func_pth1, NULL);
if(iRet)
{
printf("create pthread fail\n");
}
printf("---pthread_t = %u\n", (unsigned int)pth1);
pthread_join(pth1, NULL);
return 0;
}
运行结果:
main gettid = 5100
main pthread_self = 3078461120
---pthread_t = 3078458224
child gettid = 5101
child pthread_self= 3078458224
分析:
1 pthread_self()是POSIX的实现,它的返回值是pthread_t,pthread_t在linux中实际是无符号长整型,即unsigned long。gettid是系统调用,它的返回值是pid_t,在linux上是一个无符号整型。但glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。
2 pthread_self是为了区分同一进程中不同的线程, 是由thread的实现来决定的。pthread_self返回的是同一个进程中各个线程之间的标识号,对于这个进程内是唯一的,而不同进程中,每个线程返回的pthread_self可能返回的是一样的。而gettid获取的线程id和pid是有关系的,因为在linux中线程其实也是一个进程(clone),所以它的线程ID也是pid_t类型。在一个进程中,主线程的线程id和进程id是一样的,该进程中其他的线程id则在linux系统内是唯一的,gettid是不可移植的。
3 Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。
有时候我们可能需要知道线程的真实pid。比如进程P1要向另外一个进程P2中的某个线程发送信号时,既不能使用P2的pid,更不能使用线程的pthread id,而只能使用该线程的真实pid,称为tid。
相关文章推荐
- Linux下多线程编程__线程的创建pthread_create与退出pthread_exit,等待当前线程退出pthread_join,获取线程ID pthread_self
- [Linux线程]使用pthread_create函数来创建一个线程
- Linux 线程Pthread(一)创建、终止、获取TID
- linux创建线程之pthread_create
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux获取线程ID
- linux 线程创建函数pthread_create的几个传参方式
- linux之通过ptrace获取指定pthread线程的寄存器信息
- linux获取线程ID
- linux创建线程之pthread_create
- Linux下获取线程ID的方法
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- [Linux线程]使用pthread_create函数的arg参数
- Linux获取线程id的方法学习
- linux 下获取线程ID
- [Pthread] Linux上获得线程Id的方法
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- 【Linux学习】pthread_create主线程与创建的新线程之间退出关系
- linux 线程创建函数pthread_create的几个传参方式
- Linux中使用ptrace获取pthread线程的寄存器信息