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       └────────────────────────────────────────────┴───────────────┴─────────┘
42

CONFORMING TO

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

NOTES

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

EXAMPLES

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

SEE ALSO

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

COLOPHON

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