1PTHREAD_GETCPUCLOCKID(3)   Linux Programmer's Manual  PTHREAD_GETCPUCLOCKID(3)
2
3
4

NAME

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

SYNOPSIS

9       #include <pthread.h>
10       #include <time.h>
11
12       int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
13
14       Compile and link with -pthread.
15

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

VERSIONS

31       This function is available in glibc since version 2.2.
32

ATTRIBUTES

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

CONFORMING TO

43       POSIX.1-2001, POSIX.1-2008.
44

NOTES

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

EXAMPLES

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

SEE ALSO

140       clock_gettime(2),   clock_settime(2),   timer_create(2),  clock_getcpu‐
141       clockid(3), pthread_self(3), pthreads(7), time(7)
142

COLOPHON

144       This page is part of release 5.10 of the Linux  man-pages  project.   A
145       description  of  the project, information about reporting bugs, and the
146       latest    version    of    this    page,    can     be     found     at
147       https://www.kernel.org/doc/man-pages/.
148
149
150
151Linux                             2020-11-01          PTHREAD_GETCPUCLOCKID(3)
Impressum