C Timer Callback

Interested in something like JavaScript setTimeout in C on both UNIX and Windows.

Basically, I want:

start_timer(&function_pointer, int time_in_secs) 

or as close to him as possible.

Also, something like setInterval would be nice (where it calls a callback every n seconds), but it can be implemented using setTimeout :)

+6
c windows unix posix timer
source share
5 answers

SetTimer in Win32 with TimerProc as a call back.

 /* calls TimerProc once every 60000 milliseconds */ SetTimer(NULL, 1, 60000, TimerProc); 
+8
source share

You might want to try the POSIX interval timers, timer_create and timer_settime, as it allows you to directly specify the callback function without using signals. So that the timer expires only once (and does not repeat):

timer_settime : the reset value of the timer must be set to the value specified by the it_interval member value. When the timer is equipped with a non-zero it_interval, periodic (or repeating).

Here is extensive documentation on using these timers with a good example from the Linux programming guide at kernel.org:

timer_create - create a POSIX timer for each process

+5
source share

For UNIX man setitimer .

+2
source share

Some information about alarm () and setitimer () can be found here . Please note that the upcall function is a signal handler, therefore it is subject to all the restrictions and restrictions associated with signal handlers, as described in more detail here .

As noted in the first link, alarm () is based on POSIX, setitimer () is not (albeit more flexible).

+1
source share

Try glib. If you need portable code, there is a high probability that you will not be able to write something more portable than them;)

http://library.gnome.org/devel/glib/2.20/glib-The-Main-Event-Loop.html#g-timeout-add

-one
source share

All Articles