pthread_setname_np
Table des matières
Retour à l'index
NOM
pthread_setname_np, pthread_getname_np - Définir ou obtenir le nom d'un
thread
BIBLIOTHÈQUE
Bibliothèque de threads POSIX (libpthread, -lpthread)
SYNOPSIS
#define _GNU_SOURCE /* Consultez feature_test_macros(7) */
#include <pthread.h>
int pthread_setname_np(pthread_t thread, const char *nom);
int pthread_getname_np(pthread_t thread, char nom[.taille], size_t taille);
DESCRIPTION
By default, all the threads created using pthread_create() inherit the
program name. The pthread_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'). The thread argument specifies the thread whose name
is to be changed; name specifies the new name.
La fonction pthread_getname_np() permet de récupérer le nom du
thread. L'argument thread indique le thread dont le nom doit être
récupéré. Le tampon nom est utilisé pour renvoyer le nom du thread ;
taille indique le nombre d'octets disponibles dans nom. La longueur du
tampon indiqué dans nom doit être d'au moins 16 caractères. Le nom du
thread renvoyé dans le tampon de retour est terminé par caractère nul.
VALEUR RENVOYÉE
En cas de succès, ces fonctions renvoient 0 ; en cas d'erreur, elles
renvoient un code d'erreur non nul.
ERREURS
La fonction pthread_setname_np() peut échouer avec l'erreur suivante :
- ERANGE
-
La longueur de la chaîne vers laquelle pointe nom dépasse la limite
autorisée.
La fonction pthread_getname_np() peut échouer avec l'erreur suivante :
- ERANGE
-
Le tampon indiqué par nom et taille a une taille insuffisante pour
contenir le nom du thread.
Si l'une de ces fonctions ne parvient pas à ouvrir
/proc/self/task/tid/comm, alors l'appel peut échouer en retournant
l'une des erreurs décrites dans open(2)
ATTRIBUTS
Pour une explication des termes utilisés dans cette section, consulter
attributes(7).
| Interface | Attribut | Valeur
|
|
pthread_setname_np(),
pthread_getname_np()
| Sécurité des threads | MT-Safe
|
STANDARDS
GNU ; d'où le suffixe « _np » (non portable) dans leur nom.
HISTORIQUE
glibc 2.12.
NOTES
pthread_setname_np() écrit en interne dans le fichier comm du thread
(dans le système de fichiers /proc) :
/proc/self/task/tid/comm. pthread_getname_np() récupère le nom du
thread à ce même endroit.
EXEMPLES
Le programme ci-dessous illustre l'utilisation de pthread_setname_np() et
de pthread_getname_np().
La session d'interpréteur suivant montre un échantillon d'exécution du
programme :
$ ./a.out
Thread créé. Le nom par défaut est : a.out
Le nom du thread après nommage est THREADFOO.
haZ # Suspendre l'exécution du programme
[1]+ Stoppé ./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
THREADFOO
Source du programme
#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);
}
VOIR AUSSI
prctl(2), pthread_create(3), pthreads(7)
TRADUCTION
La traduction française de cette page de manuel a été créée par
Christophe Blaess <https://www.blaess.fr/christophe/>,
Stéphan Rafin <stephan.rafin@laposte.net>,
Thierry Vignaud <tvignaud@mandriva.com>,
François Micaux,
Alain Portal <aportal@univ-montp2.fr>,
Jean-Philippe Guérard <fevrier@tigreraye.org>,
Jean-Luc Coulon (f5ibh) <jean-luc.coulon@wanadoo.fr>,
Julien Cristau <jcristau@debian.org>,
Thomas Huriaux <thomas.huriaux@gmail.com>,
Nicolas François <nicolas.francois@centraliens.net>,
Florentin Duneau <fduneau@gmail.com>,
Simon Paillard <simon.paillard@resel.enst-bretagne.fr>,
Denis Barbier <barbier@debian.org>,
David Prévot <david@tilapin.org>,
Frédéric Hantrais <fhantrais@gmail.com>
et
Jean-Pierre Giraud <jean-pierregiraud@neuf.fr>
Cette traduction est une documentation libre ; veuillez vous reporter à la
GNU General Public License version 3
concernant les conditions de copie et
de distribution. Il n'y a aucune RESPONSABILITÉ LÉGALE.
Si vous découvrez un bogue dans la traduction de cette page de manuel,
veuillez envoyer un message à
Index
- NOM
-
- BIBLIOTHÈQUE
-
- SYNOPSIS
-
- DESCRIPTION
-
- VALEUR RENVOYÉE
-
- ERREURS
-
- ATTRIBUTS
-
- STANDARDS
-
- HISTORIQUE
-
- NOTES
-
- EXEMPLES
-
- Source du programme
-
- VOIR AUSSI
-
- TRADUCTION
-
This document was created by
man2html,
using the manual pages.
Time: 05:06:22 GMT, September 19, 2025