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
    评论
线程中的pthread_create()函数是用来创建一个新的线程的。它的函数原型为int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)。这个函数通过传入四个参数来创建新的线程。第一个参数thread是一个指向pthread_t类型的指针,用来保存新线程的ID。第二个参数attr是一个指向pthread_attr_t类型的指针,用来指定新线程的属性,如果为NULL则使用默认属性。第三个参数start_routine是一个函数指针,指向新线程将要执行的函数。最后一个参数arg是一个指向任意类型的指针,用来传递给start_routine函数的参数。调用成功返回0,失败返回错误编号。 每个线程都有自己的独立栈来保存线程运行时形成的临时数据。而上下文中保存的是CPU调度时存放在寄存器中的临时数据。线程的标识符类型为pthread_t,可以使用pthread_self()函数获取当前线程的ID,类似于使用getpid()函数获取进程的ID。[2,3]<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Linux线程:创建(pthread_create),等待(pthread_join),退出(pthread_exit)](https://blog.csdn.net/m0_74985965/article/details/128815940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值