pthread_getcpuclockid
Table des matières
Retour à l'index
NOM
pthread_getcpuclockid - Récupérer l'identifiant d'horloge de temps CPU d'un
thread
BIBLIOTHÈQUE
Bibliothèque de threads POSIX (libpthread, -lpthread)
SYNOPSIS
#include <pthread.h>
#include <time.h>
int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
DESCRIPTION
La fonction pthread_getcpuclockid() récupère l'identifiant d'horloge pour
l'horloge de temps processeur du thread donné dans thread et le renvoie à
l'emplacement vers lequel pointe clockid.
VALEUR RENVOYÉE
En cas de réussite, cette fonction renvoie 0 ; en cas d'erreur, elle renvoie
un numéro d'erreur non nul.
ERREURS
- ENOENT
-
Les horloges de temps CPU par thread ne sont pas pris en charge par le
système.
- ESRCH
-
Aucun fil d’exécution avec pour identifiant thread n'a pu être trouvé.
ATTRIBUTS
Pour une explication des termes utilisés dans cette section, consulter
attributes(7).
| Interface | Attribut | Valeur
|
|
pthread_getcpuclockid()
| Sécurité des threads | MT-Safe
|
STANDARDS
POSIX.1-2008.
HISTORIQUE
glibc 2.2. POSIX.1-2001.
NOTES
Quand thread se rapporte au thread appelant, cette fonction renvoie un
identifiant qui indique la même horloge que celle manipulée par
clock_gettime(2) et clock_settime(2) avec l'identifiant d'horloge
CLOCK_THREAD_CPUTIME_ID.
EXEMPLES
Le programme ci-dessous crée un thread puis utilise clock_gettime(2) pour
récupérer le temps CPU total du processus et le temps CPU utilisé par chacun
des deux threads. La session suivante montre un exemple d'exécution :
$ ./a.out
Main thread sleeping
Subthread starting infinite loop
Main thread consuming some CPU time...
Process total CPU time: 1.368
Main thread CPU time: 0.376
Subthread CPU time: 0.992
Source du programme
/* Link with "-lrt" */
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)
#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
static void *
thread_start(void *arg)
{
printf("Subthread starting infinite loop\n");
for (;;)
continue;
}
static void
pclock(char *msg, clockid_t cid)
{
struct timespec ts;
printf("%s", msg);
if (clock_gettime(cid, &ts) == -1)
handle_error("clock_gettime");
printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
}
int
main(void)
{
pthread_t thread;
clockid_t cid;
int s;
s = pthread_create(&thread, NULL, thread_start, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");
printf("Main thread sleeping\n");
sleep(1);
printf("Main thread consuming some CPU time...\n");
for (unsigned int j = 0; j < 2000000; j++)
getppid();
pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
s = pthread_getcpuclockid(pthread_self(), &cid);
if (s != 0)
handle_error_en(s, "pthread_getcpuclockid");
pclock("Main thread CPU time: ", cid);
/* The preceding 4 lines of code could have been replaced by:
pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
s = pthread_getcpuclockid(thread, &cid);
if (s != 0)
handle_error_en(s, "pthread_getcpuclockid");
pclock("Subthread CPU time: 1 ", cid);
exit(EXIT_SUCCESS); /* Terminates both threads */
}
VOIR AUSSI
clock_gettime(2), clock_settime(2), timer_create(2),
clock_getcpuclockid(3), pthread_self(3), pthreads(7), time(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-Philippe MENGUAL <jpmengual@debian.org>
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