p_thread设置的线程名称没有显示在htop中

时间:2017-07-26 17:11:10

标签: c multithreading ubuntu-16.04

我有一个多线程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,但我认为这与它无关。

1 个答案:

答案 0 :(得分:0)

我明白了。我在htop中有一个过滤器,这隐藏了我的命名线程。一旦我删除了那个过滤器,就会显示出来。

相关问题