1 .\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
2 .\" and Copyright (C) 2013 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_SETNAME_NP 3 2021-03-22 "Linux" "Linux Programmer's Manual"
28 pthread_setname_np, pthread_getname_np \- set/get the name of a thread
31 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
32 .B #include <pthread.h>
34 .BI "int pthread_setname_np(pthread_t " thread ", const char *" name );
35 .BI "int pthread_getname_np(pthread_t " thread ", char *" name ", size_t " len );
38 Compile and link with \fI\-pthread\fP.
40 By default, all the threads created using
42 inherit the program name.
44 .BR pthread_setname_np ()
45 function can be used to set a unique name for a thread,
46 which can be useful for debugging
47 multithreaded applications.
48 The thread name is a meaningful C language string, whose length is
49 restricted to 16 characters, including the terminating null byte (\(aq\e0\(aq).
52 argument specifies the thread whose name is to be changed;
54 specifies the new name.
57 .BR pthread_getname_np ()
58 function can be used to retrieve the name of the thread.
61 argument specifies the thread whose name is to be retrieved.
64 is used to return the thread name;
66 specifies the number of bytes available in
68 The buffer specified by
70 should be at least 16 characters in length.
71 The returned thread name in the output buffer will be null terminated.
73 On success, these functions return 0;
74 on error, they return a nonzero error number.
77 .BR pthread_setname_np ()
78 function can fail with the following error:
81 The length of the string specified pointed to by
83 exceeds the allowed limit.
86 .BR pthread_getname_np ()
87 function can fail with the following error:
90 The buffer specified by
94 is too small to hold the thread name.
96 If either of these functions fails to open
97 .IR /proc/self/task/[tid]/comm ,
98 then the call may fail with one of the errors described in
101 These functions first appeared in glibc in version 2.12.
103 For an explanation of the terms used in this section, see
111 Interface Attribute Value
113 .BR pthread_setname_np (),
114 .BR pthread_getname_np ()
115 T} Thread safety MT-Safe
121 These functions are nonstandard GNU extensions;
122 hence the suffix "_np" (nonportable) in the names.
124 .BR pthread_setname_np ()
125 internally writes to the thread-specific
130 .IR /proc/self/task/[tid]/comm .
131 .BR pthread_getname_np ()
132 retrieves it from the same location.
134 The program below demonstrates the use of
135 .BR pthread_setname_np ()
137 .BR pthread_getname_np ().
139 The following shell session shows a sample run of the program:
144 Created a thread. Default name is: a.out
145 The thread name after setting it is THREADFOO.
146 \fB\(haZ\fP # Suspend the program
148 .RB "$ " "ps H \-C a.out \-o \(aqpid tid cmd comm\(aq"
150 5990 5990 ./a.out a.out
151 5990 5991 ./a.out THREADFOO
152 .RB "$ " "cat /proc/5990/task/5990/comm"
154 .RB "$ " "cat /proc/5990/task/5991/comm"
171 #define errExitEN(en, msg) \e
172 do { errno = en; perror(msg); \e
173 exit(EXIT_FAILURE); } while (0)
176 threadfunc(void *parm)
178 sleep(5); // allow main program to set the thread name
183 main(int argc, char *argv[])
187 char thread_name[NAMELEN];
189 rc = pthread_create(&thread, NULL, threadfunc, NULL);
191 errExitEN(rc, "pthread_create");
193 rc = pthread_getname_np(thread, thread_name, NAMELEN);
195 errExitEN(rc, "pthread_getname_np");
197 printf("Created a thread. Default name is: %s\en", thread_name);
198 rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
200 errExitEN(rc, "pthread_setname_np");
204 rc = pthread_getname_np(thread, thread_name,
205 (argc > 2) ? atoi(argv[1]) : NAMELEN);
207 errExitEN(rc, "pthread_getname_np");
208 printf("The thread name after setting it is %s.\en", thread_name);
210 rc = pthread_join(thread, NULL);
212 errExitEN(rc, "pthread_join");
222 .BR pthread_create (3),