DOKK / manpages / debian 11 / manpages-fr-dev / pthread_getcpuclockid.3.fr
PTHREAD_GETCPUCLOCKID(3) Manuel du programmeur Linux PTHREAD_GETCPUCLOCKID(3)

pthread_getcpuclockid - Récupérer l'identifiant d'horloge de temps CPU d'un thread

#include <pthread.h>
#include <time.h>
int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
Compiler et éditer les liens avec -pthreads.

The pthread_getcpuclockid() function obtains the ID of the CPU-time clock of the thread whose ID is given in thread, and returns it in the location pointed to by clockid.

En cas de réussite, cette fonction renvoie 0 ; en cas d'erreur, elle renvoie un numéro d'erreur non nul.

Les horloges de temps CPU par thread ne sont pas pris en charge par le système.
Aucun fil d’exécution avec pour identifiant thread n'a pu être trouvé.

Cette fonction est disponible dans la glibc depuis la version 2.2.

Pour une explication des termes utilisés dans cette section, consulter attributes(7).

Interface Attribut Valeur
pthread_getcpuclockid() Sécurité des threads MT-Safe

POSIX.1-2001, POSIX.1-2008.

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.

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

/* Effectuez l'édition des liens avec l'option "-lrt" */
#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.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(int argc, char *argv[]) {
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 (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 */ }

clock_gettime(2), clock_settime(2), timer_create(2), clock_getcpuclockid(3), pthread_self(3), pthreads(7), time(7)

Cette page fait partie de la publication 5.10 du projet man-pages Linux. Une description du projet et des instructions pour signaler des anomalies et la dernière version de cette page peuvent être trouvées à l'adresse https://www.kernel.org/doc/man-pages/.

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> et Frédéric Hantrais <fhantrais@gmail.com>

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 à debian-l10n-french@lists.debian.org.

1 novembre 2020 Linux