【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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM