c语言中,获取线程id

发布时间 2023-12-19 14:26:33作者: He_LiangLiang

 

 

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

void *printThreadId(void *arg) {
    pid_t tid = syscall(SYS_gettid);
    printf("Thread ID: %d\n", tid);
    return NULL;
}

int main() {
    pthread_t t1, t2;

    // 创建两个线程
    pthread_create(&t1, NULL, printThreadId, NULL);
    pthread_create(&t2, NULL, printThreadId, NULL);

    // 等待线程完成
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;
}