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 errno situation, 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 ID thread could 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