2 .\" Copyright (C) 2012 Chandan Apsangi <chandan.jc@gmail.com>
3 .\" and Copyright (C) 2013 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_setname_np 3 (date) "Linux man-pages (unreleased)"
9 pthread_setname_np, pthread_getname_np \- set/get the name of a thread
12 .RI ( libpthread ", " \-lpthread )
15 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
16 .B #include <pthread.h>
18 .BI "int pthread_setname_np(pthread_t " thread ", const char *" name );
19 .BI "int pthread_getname_np(pthread_t " thread ", char " name [. size "], \
23 By default, all the threads created using
25 inherit the program name.
27 .BR pthread_setname_np ()
28 function can be used to set a unique name for a thread,
29 which can be useful for debugging
30 multithreaded applications.
31 The thread name is a meaningful C language string,
32 whose length is restricted to 16 characters,
33 including the terminating null byte (\[aq]\e0\[aq]).
36 argument specifies the thread whose name is to be changed;
38 specifies the new name.
41 .BR pthread_getname_np ()
42 function can be used to retrieve the name of the thread.
45 argument specifies the thread whose name is to be retrieved.
48 is used to return the thread name;
50 specifies the number of bytes available in
52 The buffer specified by
54 should be at least 16 characters in length.
55 The returned thread name in the output buffer will be null terminated.
57 On success, these functions return 0;
58 on error, they return a nonzero error number.
61 .BR pthread_setname_np ()
62 function can fail with the following error:
65 The length of the string specified pointed to by
67 exceeds the allowed limit.
70 .BR pthread_getname_np ()
71 function can fail with the following error:
74 The buffer specified by
78 is too small to hold the thread name.
80 If either of these functions fails to open
81 .IR /proc/self/task/ tid /comm ,
82 then the call may fail with one of the errors described in
85 For an explanation of the terms used in this section, see
91 Interface Attribute Value
95 .BR pthread_setname_np (),
96 .BR pthread_getname_np ()
97 T} Thread safety MT-Safe
101 hence the suffix "_np" (nonportable) in the names.
105 .BR pthread_setname_np ()
106 internally writes to the thread-specific
111 .IR /proc/self/task/ tid /comm .
112 .BR pthread_getname_np ()
113 retrieves it from the same location.
115 The program below demonstrates the use of
116 .BR pthread_setname_np ()
118 .BR pthread_getname_np ().
120 The following shell session shows a sample run of the program:
125 Created a thread. Default name is: a.out
126 The thread name after setting it is THREADFOO.
127 \fB\[ha]Z\fP # Suspend the program
129 .RB "$ " "ps H \-C a.out \-o \[aq]pid tid cmd comm\[aq]"
131 5990 5990 ./a.out a.out
132 5990 5991 ./a.out THREADFOO
133 .RB "$ " "cat /proc/5990/task/5990/comm"
135 .RB "$ " "cat /proc/5990/task/5991/comm"
141 .\" SRC BEGIN (pthread_setname_np.c)
155 threadfunc(void *parm)
157 sleep(5); // allow main program to set the thread name
162 main(int argc, char *argv[])
166 char thread_name[NAMELEN];
168 rc = pthread_create(&thread, NULL, threadfunc, NULL);
170 errc(EXIT_FAILURE, rc, "pthread_create");
172 rc = pthread_getname_np(thread, thread_name, NAMELEN);
174 errc(EXIT_FAILURE, rc, "pthread_getname_np");
176 printf("Created a thread. Default name is: %s\en", thread_name);
177 rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");
179 errc(EXIT_FAILURE, rc, "pthread_setname_np");
183 rc = pthread_getname_np(thread, thread_name, NAMELEN);
185 errc(EXIT_FAILURE, rc, "pthread_getname_np");
186 printf("The thread name after setting it is %s.\en", thread_name);
188 rc = pthread_join(thread, NULL);
190 errc(EXIT_FAILURE, rc, "pthread_join");
201 .BR pthread_create (3),