以前介绍过其中的一种方法:
http://blog.csdn.net/caspiansea/article/details/12073571
现在介绍另外一种方法:
使用pthread_setname_np
#define _GNU_SOURCE
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#define NAMELEN 16
#define errExitEN(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); \
} while (0)
static void *
threadfunc(void *parm)
{
while(1)
sleep(5); // allow main program to set the thread name
return NULL;
}
int
main(int argc, char **argv)
{
pthread_t thread;
int rc;
char thread_name[NAMELEN];
rc = pthread_create(&thread, NULL, threadfunc, NULL);
if (rc != 0)
errExitEN(rc, "pthread_create");
printf("Created a thread. Default name is: %s\n", thread_name);
rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
if (rc != 0)
errExitEN(rc, "pthread_setname_np");
rc = pthread_getname_np(thread, thread_name, NAMELEN);
if (rc != 0)
errExitEN(rc, "pthread_getname_np");
sleep(2);
rc = pthread_getname_np(thread, thread_name,
(argc > 2) ? atoi(argv[1]) : NAMELEN);
if (rc != 0)
errExitEN(rc, "pthread_getname_np");
printf("The thread name after setting it is %s.\n", thread_name);
rc = pthread_join(thread, NULL);
if (rc != 0)
errExitEN(rc, "pthread_join");
printf("Done\n");
exit(EXIT_SUCCESS);
}
gcc 1.c -lpthread -g
$ ps H -C a.out -o 'pid tid cmd comm'
PID TID CMD COMMAND
21377 21377 ./a.out a.out
21377 21378 ./a.out THREADFOO
其内部实现是, 直接写proc文件 /proc/self/task/[tid]/comm