当前位置: 首页 > news >正文

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

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 threadsId   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对应
http://www.dtcms.com/a/312092.html

相关文章:

  • 装 饰 器 模 式
  • 深入 Go 底层原理(七):逃逸分析
  • C++ 11 模板萃取
  • 丑数-优先队列/三指针/动态规划
  • Linux 动静态库的制作和使用
  • 深度剖析PyTorch torch.compile的性能曲线与优化临界点
  • SpringBoot 01 IOC
  • PyTorch 张量核心操作——比较、排序与数据校验
  • java实现运行SQL脚本完成数据迁移
  • 通俗易懂解释Java8 HashMap
  • Rust进阶-part1-智能指针概述-box指针
  • 【多模态】DPO学习笔记
  • 嵌入式文件系统
  • Java中Lambda 表达式的解释
  • PCB铜浆塞孔工艺流程
  • 如何快速解决PDF解密新方法?
  • 使用C++实现日志(1)
  • 疏老师-python训练营-Day33 MLP神经网络的训练
  • AbstractExecutorService:Java并发核心模板解析
  • 深入 Go 底层原理(一):Slice 的实现剖析
  • 二叉树链式结构的实现
  • lesson31:Python异常处理完全指南:从基础到高级实践
  • 乌鸫科技前端二面
  • Go语言中的闭包详解
  • OpenCV学习 day3
  • stm32是如何实现电源控制的?
  • 如何防止内存攻击(Buffer Overflow, ROP)
  • 髋臼方向的定义与测量-I
  • u-boot启动过程(NXP6ULL)
  • android studio 安装Flutter