By default, all the threads created using pthread_create() inherit the program name.
1.prctl - operations on a process
The prctl() system call was introduced in Linux 2.1.57.
头文件:
#include <sys/prctl.h>
函数原型:
int prctl(int option, unsigned long arg2, unsigned long arg3,
unsigned long arg4, unsigned long arg5);
用例:
prctl(PR_SET_NAME, (unsigned long)"thread_name", 0, 0, 0);
char thread_name[MAX_THREAD_NAME_SIZE];
prctl(PR_GET_NAME, (unsigned long)thread_name, 0, 0, 0);
2.pthread_setname_np, pthread_getname_np - set/get the name of a thread
These functions first appeared in glibc in version 2.12.
头文件:
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <pthread.h>
函数原型:
int pthread_setname_np(pthread_t thread, const char *name);
int pthread_getname_np(pthread_t thread, char *name, size_t len);
用例:
if(pthread_setname_np(tcp_thread, "thread_name") != 0)
{
IPACMERR("unable to set thread name\n");
}
char thread_name[256];
pthread_getname_np(pthread_self(), thread_name, sizeof(thread_name));
链接选项:
Compile and link with -pthread.