CLOCK_MONOTONIC

The POSIX people thought about this, and the solution they came up with was another clock source called CLOCK_MONOTONIC.

When you create a timer object with timer_create(), you can tell it which clock source to use.

The way CLOCK_MONOTONIC works is that its timebase is never adjusted. The impact of that is that regardless of what time it is in the real world, if you base a timer on CLOCK_MONOTONIC and add 30 seconds to it (and then do whatever adjustments you want to the time), the timer will expire in 30 elapsed seconds.

The clock source CLOCK_MONOTONIC has the following characteristics:

Note: The important thing about the clock starting at zero is that this is a different “epoch” (or “base”) than CLOCK_REALTIME's epoch of Jan 1 1970, 00:00:00 GMT. So, even though both clocks run at the same rate, their values are not interchangeable.

Functions that don't let you choose the clock source—such as pthread_mutex_timedlock()—use CLOCK_REALTIME. This is built in to the function and isn't something that you can change.