首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我可以在pthreads / Linux中设置线程的名称吗?

我可以在pthreads / Linux中设置线程的名称吗?
EN

Stack Overflow用户
提问于 2018-03-15 07:16:36
回答 2查看 0关注 0票数 0

有什么方法可以在Linux中设置线程的名称?

EN

回答 2

Stack Overflow用户

发布于 2018-03-15 16:16:02

通过prctl(2)选项使用该功能PR_SET_NAME

由于线程在Linux上是轻量级进程(LWP),因此在这种情况下一个线程就是一个进程。

你可以看到线程名用ps -o cmd/proc/$PID/stat()

4223 (kjournald) S 1 1 1 0...
票数 0
EN

Stack Overflow用户

发布于 2018-03-15 16:47:45

从glibc v2.12开始,你可以使用pthread_setname_nppthread_getname_np设置/获取线程名称。

这些接口在一些其他POSIX系统(BSD,QNX,Mac)上可用,形式稍有不同。

设置名称就像这样:

#include <pthread.h>  // or maybe <pthread_np.h> for some OSes

// Linux
int pthread_setname_np(pthread_t thread, const char *name);

// NetBSD: name + arg work like printf(name, arg)
int pthread_setname_np(pthread_t thread, const char *name, void *arg);

// FreeBSD & OpenBSD: function name is slightly different, and has no return value
void pthread_set_name_np(pthread_t tid, const char *name);

// Mac OS X: must be set from within the thread (can't specify thread ID)
int pthread_setname_np(const char*);

你还可以这样用:

#include <pthread.h>  // or <pthread_np.h> ?

// Linux, NetBSD:
int pthread_getname_np(pthread_t th, char *buf, size_t len);
// some implementations don't have a safe buffer (see MKS/IBM below)
int pthread_getname_np(pthread_t thread, const char **name);
int pthread_getname_np(pthread_t thread, char *name);

// FreeBSD & OpenBSD: dont' seem to have getname/get_name equivalent?
// but I'd imagine there's some other mechanism to read it directly for say gdb

// Mac OS X:
int pthread_getname_np(pthread_t, char*, size_t);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/-100003624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档