1pthread_getcpuclockid(3)   Library Functions Manual   pthread_getcpuclockid(3)
2
3
4

NAME

6       pthread_getcpuclockid - retrieve ID of a thread's CPU time clock
7

LIBRARY

9       POSIX threads library (libpthread, -lpthread)
10

SYNOPSIS

12       #include <pthread.h>
13       #include <time.h>
14
15       int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
16

DESCRIPTION

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

RETURN VALUE

23       On success, this function returns 0; on error, it returns a nonzero er‐
24       ror number.
25

ERRORS

27       ENOENT Per-thread CPU time clocks are not supported by the system.
28
29       ESRCH  No thread with the ID thread could be found.
30

ATTRIBUTES

32       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
33       tributes(7).
34
35       ┌────────────────────────────────────────────┬───────────────┬─────────┐
36Interface                                   Attribute     Value   
37       ├────────────────────────────────────────────┼───────────────┼─────────┤
38pthread_getcpuclockid()                     │ Thread safety │ MT-Safe │
39       └────────────────────────────────────────────┴───────────────┴─────────┘
40

STANDARDS

42       POSIX.1-2008.
43

HISTORY

45       glibc 2.2.  POSIX.1-2001.
46

NOTES

48       When  thread  refers  to  the  calling thread, this function returns an
49       identifier that refers to the  same  clock  manipulated  by  clock_get‐
50       time(2)    and    clock_settime(2)    when    given    the   clock   ID
51       CLOCK_THREAD_CPUTIME_ID.
52

EXAMPLES

54       The program below creates a thread and then  uses  clock_gettime(2)  to
55       retrieve  the  total process CPU time, and the per-thread CPU time con‐
56       sumed by the two threads.  The following shell session shows an example
57       run:
58
59           $ ./a.out
60           Main thread sleeping
61           Subthread starting infinite loop
62           Main thread consuming some CPU time...
63           Process total CPU time:    1.368
64           Main thread CPU time:      0.376
65           Subthread CPU time:        0.992
66
67   Program source
68
69       /* Link with "-lrt" */
70
71       #include <errno.h>
72       #include <pthread.h>
73       #include <stdint.h>
74       #include <stdio.h>
75       #include <stdlib.h>
76       #include <string.h>
77       #include <time.h>
78       #include <unistd.h>
79
80       #define handle_error(msg) \
81               do { perror(msg); exit(EXIT_FAILURE); } while (0)
82
83       #define handle_error_en(en, msg) \
84               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
85
86       static void *
87       thread_start(void *arg)
88       {
89           printf("Subthread starting infinite loop\n");
90           for (;;)
91               continue;
92       }
93
94       static void
95       pclock(char *msg, clockid_t cid)
96       {
97           struct timespec ts;
98
99           printf("%s", msg);
100           if (clock_gettime(cid, &ts) == -1)
101               handle_error("clock_gettime");
102           printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
103       }
104
105       int
106       main(void)
107       {
108           pthread_t thread;
109           clockid_t cid;
110           int s;
111
112           s = pthread_create(&thread, NULL, thread_start, NULL);
113           if (s != 0)
114               handle_error_en(s, "pthread_create");
115
116           printf("Main thread sleeping\n");
117           sleep(1);
118
119           printf("Main thread consuming some CPU time...\n");
120           for (unsigned int j = 0; j < 2000000; j++)
121               getppid();
122
123           pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
124
125           s = pthread_getcpuclockid(pthread_self(), &cid);
126           if (s != 0)
127               handle_error_en(s, "pthread_getcpuclockid");
128           pclock("Main thread CPU time:   ", cid);
129
130           /* The preceding 4 lines of code could have been replaced by:
131              pclock("Main thread CPU time:   ", CLOCK_THREAD_CPUTIME_ID); */
132
133           s = pthread_getcpuclockid(thread, &cid);
134           if (s != 0)
135               handle_error_en(s, "pthread_getcpuclockid");
136           pclock("Subthread CPU time: 1    ", cid);
137
138           exit(EXIT_SUCCESS);         /* Terminates both threads */
139       }
140

SEE ALSO

142       clock_gettime(2),   clock_settime(2),   timer_create(2),  clock_getcpu‐
143       clockid(3), pthread_self(3), pthreads(7), time(7)
144
145
146
147Linux man-pages 6.04              2023-03-30          pthread_getcpuclockid(3)
Impressum