指定线程的名字

为了能方便的区分一个进程中的每个线程,可以通过prctl()给每个线程取个名字。这样在会创建多个线程的程序执行过程中,就能知道一个pid或tid对应的是哪个线程,对调试程序有一定帮助。

prctl是个系统调用,可以用来读取和更改一个线程的属性。其用户态接口定义如下:

#include <sys/prctl.h>
 
int prctl(int option, unsigned long arg2, unsigned long arg3,unsigned long arg4, unsigned long arg5);

第一个参数option用来告诉prctl要对当前线程做什么操作,针对不同的操作,后面需要的参数个数也不同。

其中用来获取和修改当前线程名字的option是下面两个:

PR_SET_NAME:设置当前线程的名字
PR_GET_NAME:获得当前线程的名字

这两个option都只需要一个参数,即用来存储线程名的字符串:

int prctl(int option, unsigned long arg2);

对于arg2有如下要求:
PR_SET_NAME:arg2存放将要设置的线程名的字符指针,即(char *)arg2。名字的长度最大为15字节,且应该以'\0'结尾。如果传入的字符串长度大于15字节,则字符串将被截断。
PR_GET_NAME:arg2需要是一个已经分配空间的字符指针,且长度不小于16。prctl成功返回后,arg2被赋值为当前线程名,以'\0'结尾。

prctl()执行成功返回0,失败返回-1,并设置errno。

注:prctl()只能设置/获取当前线程的名字,在glibc 2.12之后的版本中提供了两个扩展的接口pthread_setname_np()和pthread_getname_np(),可以在进程中设置和读取其他线程的名字。

代码示例:

#include<stdio.h>
#include<stdlib.h>
#include <sys/prctl.h>

void*  thread1(void* arg)
{
   prctl(PR_SET_NAME,"THREAD1");
   while(1)
   {
    printf("thread1\n");
    sleep(1000);
    }
}

int main()
{
  pthread_t th1;
  void* retval;
  pthread_create(&th1,NULL,thread1,NULL);

  printf("main thread\n");
  pthread_join(&th1,&retval);
}

这样就可以将线程的名称设置为“THREAD1”。

猜你喜欢

转载自www.cnblogs.com/xiaojianliu/p/9716731.html
今日推荐