翻译:How to name a thread in Linux?

Stack overflow地址:c++ - How to name a thread in Linux? - Stack Overflow


翻译:

我有一个用C/C++写的多线程程序。我为我的线程选择了名字(c# - Naming conventions for threads? - Stack Overflow),为了帮助调试,我想要这些名字在 GDB、top等中是可见的。这是可能的吗,如果可以应该怎么做呢?

(这里有很多要知道线程名称的原因(multithreading - Thread names--when do you need to know them? - Stack Overflow)。现在我想要知道那个线程占用了50%的CPU(top记录的)。并且在我调试的时候,经常需要切换不同的当前线程,我不得不使用“thread apply all bt”,然后看一整页的调试信息来找到正确的线程)。

Windows的解决方案在这(Download Visual Studio 2005 Retired documentation from Official Microsoft Download Center),Linux的呢?


Answers1:

POSIX的线程?

这显然不能编译,但是可以给你一个大方向。我甚至不确定是不是正确的 PR_命令,但是我认为是。这是一段...

#include

  prctl(PR_SET_NAME," terminated string",0,0,0)


Answers2:

如果你使用像ACE的线程库的话,在创建新线程的时候有一个指定线程名字的方法。

BSD Unix也有 pthread_set_name_np的调用。

否则你可以使用 prctl像 Fusspawn提到的那样。

你可能感兴趣的:(翻译:How to name a thread in Linux?)