linux 中的线程是基于进程实现的,每个线程都会有一个进程对应,通过gettid()可以获取到该进程id。
另外,通过pthread_self()获取到的是POSIX thread id。

下面简单举个例子。

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/syscall.h> 

#define THREAD_STACK_SIZE 128*1024


static void error_handler(const char *arg)
{
    printf(arg);
    exit(EXIT_FAILURE);
}


static void *thread_1(void *arg)
{
    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread 1 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);
    printf("========do a lot of work====\n");

    while(1)
    {
        pause();
    }
    printf("---->thread 1 EXIT\n");
    return;     
}

static void *thread_2(void *arg)
{
    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread 2 begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);
    printf("========do a lot of work====\n");

    while(1)
    {
        pause();
    }
    printf("---->thread 2 EXIT\n");
    return; 
}

void test_thread_init(void* thread_func())
{
    pthread_t pthread_id;
    pthread_attr_t attr;

    if (pthread_attr_init(&attr) < 0)
    {
        error_handler("test_thread_init init thread attribute failed\n");
    }

    if (pthread_attr_setstacksize (&attr, THREAD_STACK_SIZE) !=0)
    {
        error_handler("test_thread_init set stack size failed\n");
    }

    if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) < 0)
    {
        error_handler("test_thread_init set setdetachstate failed\n");
    }

    if (pthread_create(&pthread_id, &attr, thread_func, NULL) < 0)
    {
        error_handler("test_thread_init create thread failed\n");
    }

    pthread_attr_destroy(&attr);
    return;
}

int main()
{

    test_thread_init(thread_1);
    test_thread_init(thread_2);

    pid_t pid = getpid();
    pthread_t pth_id = pthread_self();
    //pid_t tid = gettid();
    pid_t tid = syscall(SYS_gettid);
    printf("------thread main begin pid=%d, tid=%d, thread id=%lu-----\n", pid, tid, pth_id);

    while(1)
    {
        pause();
    }
    printf("====>main EXIT\n");
    return 0;
}

测试结果
$ gcc test.c -o test -lpthread

$ ./test
——thread main begin pid=8010, tid=8010, thread id=3075487488—–
——thread 1 begin pid=8010, tid=8011, thread id=3075484480—–
——thread 2 begin pid=8010, tid=8012, thread id=3075349312—–
========do a lot of work====
========do a lot of work====
^C

$ ps -aux | grep test
lanyang 8010 0.0 0.0 2536 316 pts/1 Sl+ 10:30 0:00 ./test

$ ls /proc/8010/task/
8010 8011 8012

从结果可以看到主线程创建了两个线程
3075484480、3075349312对应的进程ID分别为8011、8012.

由于gettid()没有在标准C库中,所以使用时要使用系统调用号去调用。系统调用号可以通过查看文件得到:
/usr/include/asm/unistd.h

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:2 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐