让我用代码澄清我的问题:
int main()
{
// call for some function which creates threads
i_create_some_threads();
// I can get PID
pid_t pid = getpid();
// need to get all threads here
// ???
}
我知道解析 /proc/PID/tasks/
但正在寻找一种在我的程序中执行此操作且无需解析目录/文件的方法。
有什么办法吗?
最佳答案
涉及的东西
ps -o nlwp <pid>
可能是你想要的。 -o 表示用户定义的输出格式,nlwp 是线程数。这将为您提供如下输出:
NLWP
6
只需尝试执行该命令并在您的代码中操作其输出。
关于c++ - 如何在 linux 上获取在 "black box"中创建的线程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28884809/