【C++】linux多线程设置线程名字

目录

1. prctl()  (只能给当前线程设置名称)

2、pthread_setname_np


1. prctl()  (只能给当前线程设置名称)

#include <sys/prctl.h>

iErr = prctl(PR_SET_NAME, “Hello_project”);

第一个参数是操作类型,指定PR_SET_NAME,即设置进程名
PR_SET_NAME:设置当前线程的名字
PR_GET_NAME:获得当前线程的名字

第二个参数是进程名字符串,长度至多16字节

prctl 缺点
prctl()只能设置/获取当前线程的名字

注意:只能设置本线程的名称,传入参数线程名超出长度,会自动截断

2、pthread_setname_np

#include <pthread.h>

pthread_setname_np(stThreadId, pstThread->pcThreadName);


由于prctl的缺点,所以pthread_setname_np()和pthread_getname_np应运而生,能设置指定线程的名称。

函数外(线程外)设置名称
std::thread t3(function_3);
pthread_setname_np(t3.native_handle(),"t3_thread");
函数内(线程内)设置名称
#include <pthread.h>
void function_3() {
  pthread_setname_np(pthread_self(),"t3_threadGo");
    while (1) {
        sleep(1);
       std::cout <<__FUNCTION__<<" i am live"<<std::endl;
    }
}
std::thread t3(function_3);

注意:

pthread_setname_np传入参数线程名超出长度,不会自动截断,而是会返回错误码ERANGE(因为是非pthread标准实现,不同操作系统可能表现不一样)。

C++ 设置线程名称_落羽的专栏-CSDN博客

测试代码

代码文件:multithreads.cpp

编译指令:g++ --std=c++11 -o multiThread -pthread multithreads.cpp

执行:./multiThread

查到多线程:top -H -p $(ps -aux|grep “multiThread”|grep -v "grep"|awk '{print $2}')

代码:

#include <iostream>
#include <thread>
#include <unistd.h>
#include <sys/prctl.h>
#include <pthread.h>


void function_1() {
   
    prctl(PR_SET_NAME,"t1-inner-set");
    while (1) {
      
      sleep(1);
      std::cout <<__FUNCTION__<<" i am live"<<std::endl;
    }
}
 
void function_2() {
    while (1) {
        sleep(1);
       std::cout <<__FUNCTION__<<" i am live"<<std::endl;
    }
}


void function_3() {
    
    pthread_setname_np(pthread_self(),"t3_inner-ptset");
    while (1) {
        sleep(1);
       std::cout <<__FUNCTION__<<" i am live"<<std::endl;
    }
}
int main() {

    std::thread t1(function_1);

    std::thread t2(function_2);
    pthread_setname_np(t2.native_handle(),"t2_outer_set");

    std::thread t3(function_3);
    
 

    t1.join();
    t2.join();
    t3.join();
    return 0;
}

结果

top -H -p $(ps -aux|grep multiThread|grep -v "grep"|awk '{print $2}')

 PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND                                                                                                                                          
 861504 root      20   0   39452   1480   1332 S  0.0  0.0   0:00.00 multiThread                                                                                                                                      
 861505 root      20   0   39452   1480   1332 S  0.0  0.0   0:00.00 t1-inner-set                                                                                                                                     
 861506 root      20   0   39452   1480   1332 S  0.0  0.0   0:00.00 t2_outer_set                                                                                                                                     
 861507 root      20   0   39452   1480   1332 S  0.0  0.0   0:00.00 t3_inner-ptset    

参考
https://blog.csdn.net/bad_good_man/article/details/48787031
 

C++ 11区别<thread> get_id()和native_handle()

在我创建的测试程序中,它们为它们的线程返回相同的int值,所以我不知道它们有什么不同。

我在Windows上使用GCC 4.8.1。

this reference

get_id返回线程的ID

native_handle返回底层实现定义线程处理

get_id返回的线程标识符实际上应该是一个类(std::thread::id),而不是数字或其他平台特定的句柄。

native_handle函数返回其名称所暗示的一个本机句柄,可以由底层操作系统线程函数使用。在Windows上,这通常是,由CreateThread返回,在POSIX平台上,它典型地为pthread_t,由pthread_create初始化。

C++ 11区别 get_id()和native_handle()http://cn.voidcc.com/question/p-zyripnme-bok.html

C++11 std::thread::id其实是一个内部类:

class thread{
...
    class id{
        ...
    };
...
};
id里面有一个私有的类似typedef unsigned long int pthread_t;的数据成员。在程序的某个地方需要一个数值的id,有std::thread::native_handle(),GCC标准库,std :: thread :: native_handle()将返回pthread_self()返回的pthread_t线程ID,(c – 如何获得std :: thread()的Linux线程ID - 编程之家

也可以用pthread.h的pthread_self()解决。

Assuming you're using GCC standard library, std::thread::native_handle() returns the pthread_t thread ID returned by pthread_self(), not the OS thread ID returned by gettid()std::thread::id() is a wrapper around that same pthread_t, and GCC's std::thread doesn't provide any way to get the OS thread ID, but you could create your own mapping:

c++ - How can you get the Linux thread Id of a std::thread() - Stack Overflow

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/120679240