linux c设置线程名字

今天咋给线程起个名字,哈哈,这个在gdb中调试的时候,将会非常有用。例如用i threads命令查看所有的线程的的时候,或者用thread apply all bt命令打印所有线程的调用栈时,能知道哪些信息对应哪个线程,还是很有必要的。

给线程起名字,两种方法,分别用prctl和pthread_setname_np。
详细使用方法参见:
https://blog.csdn.net/zhizhengguan/article/details/112062624

prctl在线程函数中使用,因为没有参数指定pthread_t类型的变量。
pthread_setname_np需要比较高的glibc版本,很可惜我的Ubuntu和板子的版本都不高,所以使用不了,只能使用最原始的prctl。

示例代码:

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <sys/prctl.h>

void *thread_1(void *args)
{
    prctl(PR_SET_NAME, (unsigned long)"thread_1");
    while ( 1 )
    {
        printf("thread 1\n");
        sleep(1);
    }
}

void *thread_2(void *args)
{
    prctl(PR_SET_NAME, (unsigned long)"thread_2");
    while ( 1 )
    {
        printf("thread 2\n");
        usleep(800*1000);
    }
}

int main(int argc, char *argv[])
{
    pthread_t t1;
    pthread_t t2;
    pthread_create(&t1, NULL, thread_1, NULL);
    pthread_create(&t2, NULL, thread_2, NULL);

#ifdef  _GNU_SOURCE
    if ( pthread_setname_np(t1, "thread_1") !=0 || pthread_setname_np(t2, "thread_2") != 0 )
    {
        printf("err: set thread name failed , errno is %d\n", errno);
    }
#else
    printf("pthread_setname_np is not supported\n");
#endif
    pthread_join(t1, NULL);
    return 0;
}

gdb中实际输出线程信息示例:

(gdb) i threads
  Id   Target Id           Frame
* 1    LWP 5978 "a.out"    0x76fc0620 in pthread_join () from /lib/libpthread.so.0
  2    LWP 5984 "thread_1" 0x76f1d060 in nanosleep () from /lib/libc.so.6
  3    LWP 5985 "thread_2" 0x76f1d060 in nanosleep () from /lib/libc.so.6
(gdb) q
A debugging session is active.
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值