tags:

views:

63

answers:

2

Hello,

gcc 4.4.3 vc++ 2008

I would like to make a timer application would be portable on windows and linux. However, would be suffice to start with.

My idea is to start a timer and set it for a specified number of seconds. When the time expires call a callback function.

Is that the best way to do this?

Many thanks,

+1  A: 

Windows and linux do timers differently. I suggest that you encapsulate the timing functionality into a class. You'll have to write the class twice (once for each platform) but then the rest of the program can be the same.

Alternatively you can use a toolkit where somebody else gas already done it for you. e.g. QT or Boost.

Michael J
+1  A: 

There are many ways to do a timer. It is not hard but you need to think exactly what you want. If you want to call a callback, you usually use a thread that sleep until your delay is elapsed, before calling your callback. If you don't want to use a thread, you can call periodically a checker function that compute the time delta.

You api will be a function taking the delay and a function pointer plus the callback parameters. It will launch a thread that will sleep for the delay, then call the callback with the given parameters.

Check general purpose libraries, they usually have timers implemented (gtk+ glib, boost::timer I think).

my2c

Edit:

For the portability part, you have of course to write two versions of your timer function. If you use thread that means it is better to use a lib. As libs give you timers ... Use a lib :)

neuro