linux 读取·设置线程名

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值