0
我有一个多线程的C应用程序,我想设置线程名称,以便它们显示在诸如htop的工具中。p_thread设置线程名称不显示在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's“显示自定义线程名称”选项?并可能“更新每个刷新过程名称”? – twalberg