gpt4 book ai didi

c - pthread 设置线程名称未显示在 htop 中

转载 作者:行者123 更新时间:2023-11-30 16:43:21 26 4
gpt4 key购买 nike

我有一个多线程 C 应用程序,我想设置线程名称,以便它们显示在 htop 等工具中。

我正在创建线程

pthread_create(&q->threads[i].thread, NULL, worker, &q->threads[i]);
//q->threads[i].thread is a pthread_t object,
//and q->threads[i] is the arg passed to worker.

在工作函数中我有

pthread_t self = pthread_self();
snprintf(name, 16, "worker-%d", data->id);
printf("The name to be set is %s\n", name);
int res = pthread_setname_np(self, name);
printf("setname returned %d\n", res);
char thread_name[16];
res = pthread_getname_np(self, thread_name, 16);
printf("Get name returned %d and shows the name is '%s'\n", res, thread_name);

当我运行代码时,我得到

The name to be set is worker-1
setname returned 0
Get name returned 0 and shows the name is 'worker-1'

对于我的每个工作线程(名称的形式为worker-X)

但是,当我在 htop 中查看结果时(我已将 htop 设置为显示线程树),所有线程都会显示父程序名称。

没有其他代码在任何地方引用线程名称,所以我看不到它在哪里被重置。我还查看了/proc/{PID} ,其中的线程名称也设置错误。所以,我相信这是我的代码的问题,但我无法弄清楚。

我正在运行 Ubuntu 16。我也使用 CMake,但我认为这与之没有任何关系。

最佳答案

我明白了。我在 htop 中有一个过滤器,它隐藏了我的命名线程。当我删除该过滤器后,它就显示了。

关于c - pthread 设置线程名称未显示在 htop 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45333405/

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