set the resolution of a clock


#include <time.h>
int clock_setres( clockid_t clock_id,
                  struct timespec *res );


The clock_setres() function sets the resolution of the clock specified by clock_id from the buffer pointed to by res. The only supported clock ID is CLOCK_REALTIME.

The res parameter points to a structure containing at least the following members:

time_t tv_sec
time_t tv_nsec
The resolution of the clock, in nanoseconds.

If the specified clock isn't a supported value, an adjustment is made to the next lowest supported value. The supported values include 0.5 msec, 1 msec, 2 msec, 5 msec, 10 msec, 25 msec, 50 msec, and 55 msec.

To convert to nanoseconds multiply the number of milliseconds (that is, msec) by 1000000.

The default timer period is set based upon the CPU speed of the processor. This can be queried using the sin utility or the qnx_osinfo() function.

CPU speed Period
< 300 50 msec
>= 300 10 msec

Setting a very short period (for example, 1 msec) on a slow machine may significantly increase system overhead.


An error occurred. errno is set to indicate the error.


The clock_id isn't CLOCK_REALTIME.


 * This program prints out the clock resolution.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MSEC ( 1000000 )

int main( void )
    struct timespec res;

/* Set the resolution      */

    res.tv_nsec = 10 * MSEC;
    if( clock_setres( CLOCK_REALTIME, &res) == -1 ) {
      perror( "clock set resolution" );
      exit( EXIT_FAILURE );

    printf( "Old resolution is %ld milliseconds.\n",
          res.tv_nsec / MSEC );

/* Get the new resolution */

    if( clock_getres( CLOCK_REALTIME, &res) == -1 ) {
      perror( "clock get resolution" );
      exit( EXIT_FAILURE );

    printf( "New resolution is %ld milliseconds.\n",
          res.tv_nsec / MSEC );

    return( EXIT_SUCCESS );



Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

clock_getres(), clock_gettime(), clock_settime(), errno, qnx_getclock(), qnx_osinfo(), qnx_setclock(), ticksize utility