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
23       error 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
35       attributes(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 <stdlib.h>
72       #include <unistd.h>
73       #include <pthread.h>
74       #include <string.h>
75       #include <errno.h>
76
77       #define handle_error(msg) \
78               do { perror(msg); exit(EXIT_FAILURE); } while (0)
79
80       #define handle_error_en(en, msg) \
81               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
82
83       static void *
84       thread_start(void *arg)
85       {
86           printf("Subthread starting infinite loop\n");
87           for (;;)
88               continue;
89       }
90
91       static void
92       pclock(char *msg, clockid_t cid)
93       {
94           struct timespec ts;
95
96           printf("%s", msg);
97           if (clock_gettime(cid, &ts) == -1)
98               handle_error("clock_gettime");
99           printf("%4ld.%03ld\n", ts.tv_sec, ts.tv_nsec / 1000000);
100       }
101
102       int
103       main(int argc, char *argv[])
104       {
105           pthread_t thread;
106           clockid_t cid;
107           int j, s;
108
109           s = pthread_create(&thread, NULL, thread_start, NULL);
110           if (s != 0)
111               handle_error_en(s, "pthread_create");
112
113           printf("Main thread sleeping\n");
114           sleep(1);
115
116           printf("Main thread consuming some CPU time...\n");
117           for (j = 0; j < 2000000; j++)
118               getppid();
119
120           pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
121
122           s = pthread_getcpuclockid(pthread_self(), &cid);
123           if (s != 0)
124               handle_error_en(s, "pthread_getcpuclockid");
125           pclock("Main thread CPU time:   ", cid);
126
127           /* The preceding 4 lines of code could have been replaced by:
128              pclock("Main thread CPU time:   ", CLOCK_THREAD_CPUTIME_ID); */
129
130           s = pthread_getcpuclockid(thread, &cid);
131           if (s != 0)
132               handle_error_en(s, "pthread_getcpuclockid");
133           pclock("Subthread CPU time: 1    ", cid);
134
135           exit(EXIT_SUCCESS);         /* Terminates both threads */
136       }
137

SEE ALSO

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

COLOPHON

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