Linux C/C++ 获取进程号、线程号和设置线程名

1 前言

在Linux开发过程中,设计多线程开发时可以将进程和线程的 id 打印出来,方便开发调试和后期查问题使用,同时也包括设置线程名。

2 函数及头文件

2.1 进程ID

#include <unistd.h>

pid_t getpid(void);

2.2 线程ID

Linux中,每个进程有一个pid,类型pid_t,由getpid()取得。Linux下的POSIX线程也有一个id,类型 pthread_t,由pthread_self()取得,该id由线程库维护,其id空间是各个进程独立的(即不同进程中的线程可能有相同的id)。Linux中的POSIX线程库实现的线程其实也是一个进程(LWP),只是该进程与主进程(启动线程的进程)共享一些资源而已,比如代码段,数据段等。

由于gettid() 在 glibc 中未实现该函数,因此我们自己可以通过 Linux 的系统调用 syscall 实现

#include <sys/unistd.h>

#define gettid() syscall(__NR_gettid)

/* 第二种 */
#include <sys/syscall.h>

#define gettid() syscall(SYS_gettid)  // #define SYS_gettid __NR_gettid 在 sys/syscall.h 中定义

当只有一个线程的时候,返回的是pid。

2.3 设置线程名

#include <prctl.h>

prctl(PR_SET_NAME, "testThread"); 

// 可以通过设置 PR_GET_NAME 获取当前线程的名字

2.4 示例

需要在线程函数中调用

#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <thread>
#include <stdio.h>
#include <string.h>

#define gettid() syscall(SYS_gettid)

void TestThreadBody(int threadNumber)
{
    char szThreadName[20];

    snprintf(szThreadName, 20, "testThread-%d", threadNumber);
    prctl(PR_SET_NAME, szThreadName); 

    memset(szThreadName, 0, 20);
    prctl(PR_GET_NAME, szThreadName); 

    printf("Thread[%s] pid:%u, tid:%u\n", szThreadName, (unsigned int)getpid(), (unsigned int)gettid());
}

int main()
{
    char szThreadName[20];
    prctl(PR_SET_NAME, "mainThread"); 
    prctl(PR_GET_NAME, szThreadName); 
    printf("Thread[%s] pid:%u, tid:%u\n", szThreadName, (unsigned int)getpid(), (unsigned int)gettid());

    std::thread(TestThreadBody, 1).detach();
    std::thread(TestThreadBody, 2).detach();
    std::thread(TestThreadBody, 3).detach();

    return 0;
}

原文链接:「链接」

原文链接

Linux C/C++ 获取进程号、线程号和设置线程名-QT开发中文网Linux C/C++ 获取进程号、线程号和设置线程名https://qt.0voice.com/?id=1006

猜你喜欢

转载自blog.csdn.net/QtCompany/article/details/130225335
今日推荐