p_thread 设置线程名称不显示在 htop 中
p_thread set thread name not showing up in 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.
在 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 应用程序,我想设置线程名称,以便它们显示在 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.
在 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 中有一个过滤器,它隐藏了我命名的线程。一旦我删除了那个过滤器,它就会显示出来。