Home > OS >  What are the numerical values for clockid_t macros?
What are the numerical values for clockid_t macros?

Time:10-15

I would like to pass in clockid_t macros as a compile-time argument (gcc -D CLOCK=CLOCK_REALTIME file.c ), to define the macro CLOCK in my code. How do I find out their respective integer values so I could do that?

The 4 clockid_t's I need are the ones listed on the man page: https://linux.die.net/man/3/clock_gettime

The system is Linux.

CodePudding user response:

Look in <time.h> to get the actual values.

However, note that the system-defined clock IDs can differ in value from platform to platform. For instance, CLOCK_MONOTONIC is 1 on Linux, but is 4 on FreeBSD.

So, you are better off defining your own macro values, and then map them to the appropriate clock IDs in your code using the constants in <time.h>, which are based on which platform(s) you compile for.

#include <time.h>

clockid_t GetDefinedClock()
{
    #if CLOCK == 1
    return CLOCK_REALTIME;
    #elif CLOCK == 2
    return CLOCK_MONOTONIC;
    #elif CLOCK == 3
    return CLOCK_PROCESS_CPUTIME_ID;
    #elif CLOCK == 4
    return CLOCK_THREAD_CPUTIME_ID;
    #else
    #error "NO CLOCK DEFINED!"
    return 0;
    #endif
}

clockid_t clock = GetDefinedClock();
// use clock as needed...
  • Related