Monotonic clock on OSX

后端 未结 3 462
你的背包
你的背包 2020-11-30 06:46

CLOCK_MONOTONIC does not seem available, so clock_gettime is out.

I\'ve read in some places that mach_absolute_time() might be the right way to go, but after reading

3条回答
  •  情深已故
    2020-11-30 07:29

    The Mach kernel provides access to system clocks, out of which at least one (SYSTEM_CLOCK) is advertised by the documentation as being monotonically incrementing.

    #include 
    #include 
    
    clock_serv_t cclock;
    mach_timespec_t mts;
    
    host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
    clock_get_time(cclock, &mts);
    mach_port_deallocate(mach_task_self(), cclock);
    

    mach_timespec_t has nanosecond precision. I'm not sure about the accuracy, though.

    Mac OS X supports three clocks:

    • SYSTEM_CLOCK returns the time since boot time;
    • CALENDAR_CLOCK returns the UTC time since 1970-01-01;
    • REALTIME_CLOCK is deprecated and is the same as SYSTEM_CLOCK in its current implementation.

    The documentation for clock_get_time says the clocks are monotonically incrementing unless someone calls clock_set_time. Calls to clock_set_time are discouraged as it could break the monotonic property of the clocks, and in fact, the current implementation returns KERN_FAILURE without doing anything.

提交回复
热议问题