C - p_thread设置线程名称未显示在 htop 中



>我有一个多线程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 %sn", name);
int res = pthread_setname_np(self, name);
printf("setname returned %dn", 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 中有一个过滤器,它隐藏了我的命名线程。一旦我删除了该过滤器,它就会显示出来。

最新更新