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 *clock_id);
13
14       Compile and link with -pthread.
15

DESCRIPTION

17       The  pthread_getcpuclockid()  function returns the clock ID for the CPU
18       time clock of the thread thread.
19

RETURN VALUE

21       On success, this function returns 0; on error,  it  returns  a  nonzero
22       error number.
23

ERRORS

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

VERSIONS

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

CONFORMING TO

33       POSIX.1-2001.
34

NOTES

36       When  thread  refers  to  the  calling thread, this function returns an
37       identifier that refers to the  same  clock  manipulated  by  clock_get‐
38       time(2)    and    clock_settime(2)    when    given    the   clock   ID
39       CLOCK_THREAD_CPUTIME_ID.
40

EXAMPLE

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

SEE ALSO

128       clock_gettime(2),   clock_settime(2),   timer_create(2),  clock_getcpu‐
129       clockid(3), pthread_self(3), pthreads(7), time(7)
130

COLOPHON

132       This page is part of release 3.25 of the Linux  man-pages  project.   A
133       description  of  the project, and information about reporting bugs, can
134       be found at http://www.kernel.org/doc/man-pages/.
135
136
137
138Linux                             2009-02-08          PTHREAD_GETCPUCLOCKID(3)
Impressum