1PTHREAD_SETNAME_NP(3)      Linux Programmer's Manual     PTHREAD_SETNAME_NP(3)
2
3
4

NAME

6       pthread_setname_np, pthread_getname_np - set/get the name of a thread
7

SYNOPSIS

9       #define _GNU_SOURCE             /* See feature_test_macros(7) */
10       #include <pthread.h>
11
12       int pthread_setname_np(pthread_t thread, const char *name);
13       int pthread_getname_np(pthread_t thread, char *name, size_t len);
14
15       Compile and link with -pthread.
16

DESCRIPTION

18       By  default, all the threads created using pthread_create() inherit the
19       program name.  The pthread_setname_np() function can be used to  set  a
20       unique  name  for  a  thread,  which can be useful for debugging multi‐
21       threaded applications.  The thread name  is  a  meaningful  C  language
22       string, whose length is restricted to 16 characters, including the ter‐
23       minating null byte ('\0').  The thread argument  specifies  the  thread
24       whose name is to be changed; name specifies the new name.
25
26       The  pthread_getname_np()  function can be used to retrieve the name of
27       the thread.  The thread argument specifies the thread whose name is  to
28       be  retrieved.   The buffer name is used to return the thread name; len
29       specifies the number of bytes available in name.  The buffer  specified
30       by  name  should  be  at  least  16 characters in length.  The returned
31       thread name in the output buffer will be null terminated.
32

RETURN VALUE

34       On success, these functions return 0; on error, they return  a  nonzero
35       error number.
36

ERRORS

38       The pthread_setname_np() function can fail with the following error:
39
40       ERANGE The  length  of  the string specified pointed to by name exceeds
41              the allowed limit.
42
43       The pthread_getname_np() function can fail with the following error:
44
45       ERANGE The buffer specified by name and len is too small  to  hold  the
46              thread name.
47
48       If  either of these functions fails to open /proc/self/task/[tid]/comm,
49       then the call may fail with one of the errors described in open(2).
50

VERSIONS

52       These functions first appeared in glibc in version 2.12.
53

ATTRIBUTES

55       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
56       tributes(7).
57
58       ┌────────────────────────────────────────────┬───────────────┬─────────┐
59Interface                                   Attribute     Value   
60       ├────────────────────────────────────────────┼───────────────┼─────────┤
61pthread_setname_np(), pthread_getname_np()  │ Thread safety │ MT-Safe │
62       └────────────────────────────────────────────┴───────────────┴─────────┘
63

CONFORMING TO

65       These  functions are nonstandard GNU extensions; hence the suffix "_np"
66       (nonportable) in the names.
67

NOTES

69       pthread_setname_np() internally writes to the thread-specific comm file
70       under  the  /proc filesystem: /proc/self/task/[tid]/comm.  pthread_get‐
71       name_np() retrieves it from the same location.
72

EXAMPLES

74       The program below demonstrates  the  use  of  pthread_setname_np()  and
75       pthread_getname_np().
76
77       The following shell session shows a sample run of the program:
78
79           $ ./a.out
80           Created a thread. Default name is: a.out
81           The thread name after setting it is THREADFOO.
82           ^Z                           # Suspend the program
83           [1]+  Stopped           ./a.out
84           $ ps H -C a.out -o 'pid tid cmd comm'
85             PID   TID CMD                         COMMAND
86            5990  5990 ./a.out                     a.out
87            5990  5991 ./a.out                     THREADFOO
88           $ cat /proc/5990/task/5990/comm
89           a.out
90           $ cat /proc/5990/task/5991/comm
91           THREADFOO
92
93   Program source
94
95       #define _GNU_SOURCE
96       #include <pthread.h>
97       #include <stdio.h>
98       #include <string.h>
99       #include <unistd.h>
100       #include <errno.h>
101       #include <stdlib.h>
102
103       #define NAMELEN 16
104
105       #define errExitEN(en, msg) \
106                               do { errno = en; perror(msg); \
107                                    exit(EXIT_FAILURE); } while (0)
108
109       static void *
110       threadfunc(void *parm)
111       {
112           sleep(5);          // allow main program to set the thread name
113           return NULL;
114       }
115
116       int
117       main(int argc, char *argv[])
118       {
119           pthread_t thread;
120           int rc;
121           char thread_name[NAMELEN];
122
123           rc = pthread_create(&thread, NULL, threadfunc, NULL);
124           if (rc != 0)
125               errExitEN(rc, "pthread_create");
126
127           rc = pthread_getname_np(thread, thread_name, NAMELEN);
128           if (rc != 0)
129               errExitEN(rc, "pthread_getname_np");
130
131           printf("Created a thread. Default name is: %s\n", thread_name);
132           rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
133           if (rc != 0)
134               errExitEN(rc, "pthread_setname_np");
135
136           sleep(2);
137
138           rc = pthread_getname_np(thread, thread_name, NAMELEN);
139           if (rc != 0)
140               errExitEN(rc, "pthread_getname_np");
141           printf("The thread name after setting it is %s.\n", thread_name);
142
143           rc = pthread_join(thread, NULL);
144           if (rc != 0)
145               errExitEN(rc, "pthread_join");
146
147           printf("Done\n");
148           exit(EXIT_SUCCESS);
149       }
150

SEE ALSO

152       prctl(2), pthread_create(3), pthreads(7)
153

COLOPHON

155       This page is part of release 5.13 of the Linux man-pages project.  A
156       description of the project, information about reporting bugs, and the
157       latest version of this page, can be found at
158       https://www.kernel.org/doc/man-pages/.
159
160
161
162Linux                             2021-08-27             PTHREAD_SETNAME_NP(3)
Impressum