1PTHREAD_GETCPUCLOCKID(3) Linux Programmer's Manual PTHREAD_GETCPUCLOCKID(3)
2
3
4
6 pthread_getcpuclockid - retrieve ID of a thread's CPU time clock
7
9 #include <pthread.h>
10 #include <time.h>
11
12 int pthread_getcpuclockid(pthread_t thread, clockid_t *clock_id);
13
14 Compile and link with -pthread.
15
17 The pthread_getcpuclockid() function returns the clock ID for the CPU
18 time clock of the thread thread.
19
21 On success, this function returns 0; on error, it returns a nonzero
22 error number.
23
25 ENOENT Per-thread CPU time clocks are not supported by the system.
26
27 ESRCH No thread with the ID thread could be found.
28
30 This function is available in glibc since version 2.2.
31
33 For an explanation of the terms used in this section, see
34 attributes(7).
35
36 ┌────────────────────────┬───────────────┬─────────┐
37 │Interface │ Attribute │ Value │
38 ├────────────────────────┼───────────────┼─────────┤
39 │pthread_getcpuclockid() │ Thread safety │ MT-Safe │
40 └────────────────────────┴───────────────┴─────────┘
42 POSIX.1-2001, POSIX.1-2008.
43
45 When thread refers to the calling thread, this function returns an
46 identifier that refers to the same clock manipulated by clock_get‐
47 time(2) and clock_settime(2) when given the clock ID
48 CLOCK_THREAD_CPUTIME_ID.
49
51 The program below creates a thread and then uses clock_gettime(2) to
52 retrieve the total process CPU time, and the per-thread CPU time con‐
53 sumed by the two threads. The following shell session shows an example
54 run:
55
56 $ ./a.out
57 Main thread sleeping
58 Subthread starting infinite loop
59 Main thread consuming some CPU time...
60 Process total CPU time: 1.368
61 Main thread CPU time: 0.376
62 Subthread CPU time: 0.992
63
64 Program source
65
66 /* Link with "-lrt" */
67
68 #include <time.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <pthread.h>
73 #include <string.h>
74 #include <errno.h>
75
76 #define handle_error(msg) \
77 do { perror(msg); exit(EXIT_FAILURE); } while (0)
78
79 #define handle_error_en(en, msg) \
80 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
81
82 static void *
83 thread_start(void *arg)
84 {
85 printf("Subthread starting infinite loop\n");
86 for (;;)
87 continue;
88 }
89
90 static void
91 pclock(char *msg, clockid_t cid)
92 {
93 struct timespec ts;
94
95 printf("%s", msg);
96 if (clock_gettime(cid, &ts) == -1)
97 handle_error("clock_gettime");
98 printf("%4ld.%03ld\n", ts.tv_sec, ts.tv_nsec / 1000000);
99 }
100
101 int
102 main(int argc, char *argv[])
103 {
104 pthread_t thread;
105 clockid_t cid;
106 int j, s;
107
108 s = pthread_create(&thread, NULL, thread_start, NULL);
109 if (s != 0)
110 handle_error_en(s, "pthread_create");
111
112 printf("Main thread sleeping\n");
113 sleep(1);
114
115 printf("Main thread consuming some CPU time...\n");
116 for (j = 0; j < 2000000; j++)
117 getppid();
118
119 pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
120
121 s = pthread_getcpuclockid(pthread_self(), &cid);
122 if (s != 0)
123 handle_error_en(s, "pthread_getcpuclockid");
124 pclock("Main thread CPU time: ", cid);
125
126 /* The preceding 4 lines of code could have been replaced by:
127 pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
128
129 s = pthread_getcpuclockid(thread, &cid);
130 if (s != 0)
131 handle_error_en(s, "pthread_getcpuclockid");
132 pclock("Subthread CPU time: 1 ", cid);
133
134 exit(EXIT_SUCCESS); /* Terminates both threads */
135 }
136
138 clock_gettime(2), clock_settime(2), timer_create(2), clock_getcpu‐
139 clockid(3), pthread_self(3), pthreads(7), time(7)
140
142 This page is part of release 5.04 of the Linux man-pages project. A
143 description of the project, information about reporting bugs, and the
144 latest version of this page, can be found at
145 https://www.kernel.org/doc/man-pages/.
146
147
148
149Linux 2019-03-06 PTHREAD_GETCPUCLOCKID(3)