linux中pthread_t 的值与top -Hp中线程id值的区别

发布于:2025-08-03 ⋅ 阅读:(16) ⋅ 点赞:(0)

linux中pthread_t 值与top -Hp中线程id值的区别

#include <stdio.h>
#include <pthread.h>
#include <thread>

void thread_func() 
{
    printf("child thread id=0x%x\n",pthread_self());
    while(1)
    {   
        printf("hello world\n");
    }   
}


int main() 
{
    printf("main thread id=0x%x\n",pthread_self());
    //设置主线程名称
    pthread_setname_np(pthread_self(), "MainThread");
    std::thread t(thread_func);
    //设置子线程名称
    pthread_setname_np(t.native_handle(), "WorkerThread");
    t.join();
    return 0;
}
[banting@localhost test]$ g++ -g --std=c++11 test84.cpp -lpthread -o test84
[banting@localhost test]$ gdb ./test84
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-100.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/banting/test/test84...done.
(gdb) b 23
Breakpoint 1 at 0x400ec0: file test84.cpp, line 23.
(gdb) r
Starting program: /home/banting/test/./test84 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib64/libthread_db.so.1".
main thread id=0xf7fe2740
[New Thread 0x7ffff6fdb700 (LWP 1347067)]
child thread id=0xf6fdb700
hello world
hello world
Breakpoint 1, main () at test84.cpp:23
23		t.join();
(gdb) info threads
  Id   Target Id         Frame 
  2    Thread 0x7ffff6fdb700 (LWP 1347067) "WorkerThread" 0x00007ffff70c585d in write () at ../sysdeps/unix/syscall-template.S:81
* 1    Thread 0x7ffff7fe2740 (LWP 1347063) "MainThread" main () at test84.cpp:23
(gdb) quit
A debugging session is active.

	Inferior 1 [process 1347063] will be killed.

Quit anyway? (y or n) y
[banting@localhost test]$
[banting@localhost test]$ top -Hp 1347063
top - 17:42:14 up 19 days, 17:18, 22 users,  load average: 0.00, 0.01, 0.05
Threads:   2 total,   0 running,   0 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem : 16268040 total,  2925428 free,  3114704 used, 10227908 buff/cache
KiB Swap:  8257532 total,  8254336 free,     3196 used. 12482396 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND 
1347063 banting   20   0   22996   1120    912 t  0.0  0.0   0:00.00 MainThread                                                                                                                          
1347067 banting   20   0   22996   1120    912 t  0.0  0.0   0:00.00 WorkerThread  

总结

  • pthread_t的值main thread id=0xf7fe2740只与gdb调试中Thread 0x7ffff7fe2740 的后4个字节对应
  • Thread 0x7ffff7fe2740 (LWP 1347063)中(LWP 1347063) 只与top -Hp 1347063打印信息的MainThread 的线程id对应

网站公告

今日签到

点亮在社区的每一天
去签到