Movatterモバイル変換


[0]ホーム

URL:


man7.org > Linux >man-pages

Linux/UNIX system programming training


pthread_setname_np(3) — Linux manual page

NAME |LIBRARY |SYNOPSIS |DESCRIPTION |RETURN VALUE |ERRORS |ATTRIBUTES |STANDARDS |HISTORY |NOTES |EXAMPLES |SEE ALSO |COLOPHON

pthread_setname_np(3)    Library Functions Manualpthread_setname_np(3)

NAME        top

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

LIBRARY        top

       POSIX threads library (libpthread,-lpthread)

SYNOPSIS        top

#define _GNU_SOURCE/* See feature_test_macros(7) */#include <pthread.h>int pthread_setname_np(pthread_tthread, const char *name);int pthread_getname_np(size_t size;pthread_tthread, charname[size], size_tsize);

DESCRIPTION        top

       By default, all the threads created usingpthread_create() inherit       the program name.  Thepthread_setname_np() function can be used       to set a unique name for a thread, which can be useful for       debugging multithreaded applications.  The thread name is a       meaningful C language string, whose length is restricted to 16       characters, including the terminating null byte ('\0').  Thethread argument specifies the thread whose name is to be changed;name specifies the new name.       Thepthread_getname_np() function can be used to retrieve the name       of the thread.  Thethread argument specifies the thread whose       name is to be retrieved.  The buffername is used to return the       thread name;size specifies the number of bytes available inname.       The buffer specified byname should be at least 16 characters in       length.  The returned thread name in the output buffer will be       null terminated.

RETURN VALUE        top

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

ERRORS        top

       Thepthread_setname_np() function can fail with the following       error:ERANGEThe length of the string specified pointed to byname              exceeds the allowed limit.       Thepthread_getname_np() function can fail with the following       error:ERANGEThe buffer specified byname andsize is too small to hold              the thread name.       If either of these functions fails to open/proc/self/task/tid/comm, then the call may fail with one of the       errors described inopen(2).

ATTRIBUTES        top

       For an explanation of the terms used in this section, seeattributes(7).       ┌──────────────────────────────────────┬───────────────┬─────────┐       │InterfaceAttributeValue│       ├──────────────────────────────────────┼───────────────┼─────────┤       │pthread_setname_np(),                │ Thread safety │ MT-Safe │       │pthread_getname_np()                 │               │         │       └──────────────────────────────────────┴───────────────┴─────────┘

STANDARDS        top

       GNU; hence the suffix "_np" (nonportable) in the names.

HISTORY        top

       glibc 2.12.

NOTES        top

pthread_setname_np() internally writes to the thread-specificcomm       file under the/proc filesystem:/proc/self/task/tid/comm.pthread_getname_np() retrieves it from the same location.

EXAMPLES        top

       The program below demonstrates the use ofpthread_setname_np() andpthread_getname_np().       The following shell session shows a sample run of the program:           $./a.out;           Created a thread. Default name is: a.out           The thread name after setting it is THREADFOO.^Z# Suspend the program           [1]+  Stopped           ./a.out           $ps H -C a.out -o 'pid tid cmd comm'             PID   TID CMD                         COMMAND            5990  5990 ./a.out                     a.out            5990  5991 ./a.out                     THREADFOO           $cat /proc/5990/task/5990/comm           a.out           $cat /proc/5990/task/5991/comm           THREADFOOProgram source       #define _GNU_SOURCE       #include <err.h>       #include <errno.h>       #include <pthread.h>       #include <stdio.h>       #include <stdlib.h>       #include <string.h>       #include <unistd.h>       #define NAMELEN 16       static void *       threadfunc(void *parm)       {           sleep(5);          // allow main program to set the thread name           return NULL;       }       int       main(int argc, char *argv[])       {           pthread_t thread;           int rc;           char thread_name[NAMELEN];           rc = pthread_create(&thread, NULL, threadfunc, NULL);           if (rc != 0)               errc(EXIT_FAILURE, rc, "pthread_create");           rc = pthread_getname_np(thread, thread_name, NAMELEN);           if (rc != 0)               errc(EXIT_FAILURE, rc, "pthread_getname_np");           printf("Created a thread. Default name is: %s\n", thread_name);           rc = pthread_setname_np(thread, (argc > 1) ? argv[1] : "THREADFOO");           if (rc != 0)               errc(EXIT_FAILURE, rc, "pthread_setname_np");           sleep(2);           rc = pthread_getname_np(thread, thread_name, NAMELEN);           if (rc != 0)               errc(EXIT_FAILURE, rc, "pthread_getname_np");           printf("The thread name after setting it is %s.\n", thread_name);           rc = pthread_join(thread, NULL);           if (rc != 0)               errc(EXIT_FAILURE, rc, "pthread_join");           printf("Done\n");           exit(EXIT_SUCCESS);       }

SEE ALSO        top

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

COLOPHON        top

       This page is part of theman-pages (Linux kernel and C library       user-space interface documentation) project.  Information about       the project can be found at        ⟨https://www.kernel.org/doc/man-pages/⟩.  If you have a bug report       for this manual page, see       ⟨https://git.kernel.org/pub/scm/docs/man-pages/man-pages.git/tree/CONTRIBUTING⟩.       This page was obtained from the tarball man-pages-6.15.tar.gz       fetched from       ⟨https://mirrors.edge.kernel.org/pub/linux/docs/man-pages/⟩ on       2025-08-11.  If you discover any rendering problems in this HTML       version of the page, or you believe there is a better or more up-       to-date source for the page, or you have corrections or       improvements to the information in this COLOPHON (which isnot       part of the original manual page), send a mail to       man-pages@man7.orgLinux man-pages 6.15            2025-06-28pthread_setname_np(3)

Pages that refer to this page:PR_SET_NAME(2const)proc_pid_comm(5)



HTML rendering created 2025-09-06 byMichael Kerrisk, author ofThe Linux Programming Interface.

For details of in-depthLinux/UNIX system programming training courses that I teach, lookhere.

Hosting byjambit GmbH.

Cover of TLPI


[8]ページ先頭

©2009-2025 Movatter.jp