linux 线程创建 pthread_create函数 获取线程id

函数原型:

#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


分析:

pthread_self()是POSIX的实现,它的返回值是pthread_t,pthread_t在linux中实际是无符号长整型,即unsigned long。gettid是系统调用,它的返回值是pid_t,在linux上是一个无符号整型。glibc并没有实现该函数,只能通过Linux的系统调用syscall来获取。

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。



  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值