pthread_getcpuclockid(3)
π pthread_getcpuclockid(3) π
okay bestie, so this one lives in the Library Functions Manual, and it's part of the POSIX threads library (libpthread, -lpthread) π§΅β¨ β yes THAT library, the one you link with -lpthread. Come sit, we're gonna learn about clocks for threads. π°οΈ
πΈ NAME πΈ
pthread_getcpuclockid β grab the ID of a thread's CPU time clock π
π LIBRARY π
POSIX threads library (libpthread, -lpthread) π«Ά
β¨ SYNOPSIS β¨
#include <pthread.h>
#include <time.h>
int pthread_getcpuclockid(pthread_t thread, clockid_t *clockid);
π¦ DESCRIPTION π¦
So pthread_getcpuclockid() literally goes and obtains the ID of the CPU-time clock of the thread whose ID you handed it in thread π
and then it returns that ID in the location pointed to by clockid. That's it. That's the whole thing. Iconic, honestly. π
β RETURN VALUE β
- On success, this function returns 0 β slay. π
- On error, it returns a nonzero error number. π (not an
errnosituation, bestie, it hands you the error number directly, no cap.)
π ERRORS π
ENOENTβ Per-thread CPU time clocks aren't supported by the system. π The system said "we don't do that here."ESRCHβ No thread with the IDthreadcould be found. π Like, who?? Nobody, apparently.
π§ ATTRIBUTES π§
For an explanation of the terms used in this section, go peek at attributes(7) π
- Interface:
pthread_getcpuclockid() - Attribute: Thread safety
- Value:
MT-Safeπ₯ (yes, it's safe to call from multiple threads, you're welcome)
π STANDARDS π
POSIX.1-2008. π
π HISTORY π
- glibc 2.2. β
- POSIX.1-2001. π°οΈ
π NOTES π
When thread refers to the calling thread, this function returns an identifier that refers to the same clock manipulated by clock_gettime(2) and clock_settime(2) when given the clock ID CLOCK_THREAD_CPUTIME_ID. π So likeβ¦ same clock, different name tag. iykyk. π
π₯ EXAMPLES π₯
The program below creates a thread and then uses clock_gettime(2) to retrieve the total process CPU time, plus the per-thread CPU time consumed by the two threads π§΅π§΅ The following shell session shows an example run:
$ ./a.out;
Main thread sleeping
Subthread starting infinite loop
Main thread consuming some CPU time...
Process total CPU time: 1.368
Main thread CPU time: 0.376
Subthread CPU time: 0.992
π Program source π
/* Link with "-lrt" */
#include <err.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
static void *
thread_start(void *arg)
{
printf("Subthread starting infinite loop\n");
for (;;)
continue;
}
static void
pclock(char *msg, clockid_t cid)
{
struct timespec ts;
printf("%s", msg);
if (clock_gettime(cid, &ts) == -1)
err(EXIT_FAILURE, "clock_gettime");
printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
}
int
main(void)
{
pthread_t thread;
clockid_t cid;
int s;
s = pthread_create(&thread, NULL, thread_start, NULL);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_create");
printf("Main thread sleeping\n");
sleep(1);
printf("Main thread consuming some CPU time...\n");
for (unsigned int j = 0; j < 2000000; j++)
getppid();
pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
s = pthread_getcpuclockid(pthread_self(), &cid);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_getcpuclockid");
pclock("Main thread CPU time: ", cid);
/* The preceding 4 lines of code could have been replaced by:
pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
s = pthread_getcpuclockid(thread, &cid);
if (s != 0)
errc(EXIT_FAILURE, s, "pthread_getcpuclockid");
pclock("Subthread CPU time: 1 ", cid);
exit(EXIT_SUCCESS); /* Terminates both threads */
}
π«Ά SEE ALSO π«Ά
clock_gettime(2), clock_settime(2), timer_create(2), clock_getcpuclockid(3), pthread_self(3), pthreads(7), time(7)
and that's the whole page, bestie π this came from Linux man-pages 6.20, dated 2025-08-20 β go forth and clock those threads, main character π°οΈπβ¨
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: Linux-man-pages-copyleft
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms