Timer function code for Linux and Windows

Here you can find a multiplatform timer library.
It has been tested with GCC on Linux and MinGW on Windows.
It is licensed under the GPL version 2.


This is free software, it is experimental and available under the GPL License version 2.
Despite this software is intend to be usefull, there is no warranty, use this software at your own risk.

The sourcecode


int start_timer(int milliSeconds, void (*)(void))

  Starts the timer. Second argument is a pointer to your function that will be called.
  Returns 1 in case of an error.

void stop_timer(void)

  Stops the timer


Example code that demonstrates how to use the timer:
(compile with the command: gcc main.c timer.c -Wall -Wextra -s -o2 -o test_timer)


file: main.c
purpose: simple demo that demonstrates the timer-function


#include <stdio.h>
#include <stdlib.h>

#include "timer.h"

void timer_handler(void);

int var=0;

int main(void)
  if(start_timer(1000, &timer_handler))
    printf("\n timer error\n");

  printf("\npress ctl-c to quit.\n");

    if(var > 5)



void timer_handler(void)
  printf("timer: var is %i\n", var++);

Accuracy and Timer Resolution

The timer will never time out earlier than the specified timeout value and it is not guaranteed to time out at the exact value specified.
In many situations, they may time out late by a period of time that depends on the accuracy of the system timers.

The accuracy of a timer depends on the underlying operating system and hardware. Most platforms support a resolution of 1 millisecond,
though the accuracy of the timer will not equal this resolution in many real-world situations.