gpt4 book ai didi

c++ - 如何在 Linux 中命名线程?

转载 作者:IT老高 更新时间:2023-10-28 12:37:48 27 4
gpt4 key购买 nike

我有一个用 C/C++ 编写的多线程 Linux 应用程序。我有 chosen names for my threads .为了帮助调试,我希望这些名称在 GDB、“top”等中可见。这可能吗?如果可以,如何?

(有很多 reasons to know the thread name 。现在我想知道哪个线程占用了 50% 的 CPU(由 'top' 报告)。调试时我经常需要切换到不同的线程 - 目前我必须执行“thread apply all bt”,然后查看回溯输出页面以找到正确的线程)。

Windows solution is here ; Linux 是什么?

最佳答案

Posix 线程?

这显然不会编译,但它会让你知道去哪里打猎。我什至不确定它是正确的 PR_ 命令,但我认为它是正确的。好久不见……

  #include <sys/prctl.h>
prctl(PR_SET_NAME,"<null> terminated string",0,0,0)

关于c++ - 如何在 Linux 中命名线程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/778085/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com