2017-07-26 23 views
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,但我認爲這跟它沒有任何關係。

+0

您是否啓用了'htop's「顯示自定義線程名稱」選項?並可能「更新每個刷新過程名稱」? – twalberg

回答

0

我想通了。我在htop中有一個過濾器,並且隱藏了我的命名線程。一旦我刪除了該過濾器,就會顯示出來。