0

how would one implement a C++ timer function which would act like: void glutTimerFunc(unsigned int msecs,void (*func)(int value), value); but was purely WinAPI (or STL) stuff? I need it to not busy wait, though. It needs to call a function after X number of milliseconds Thank you for any/all help!

I've been looking at struct timeval tv; but I'm al little confused about how to actually implement it. It needs to be a drop in replacement for glutTimerFunc(). Thanks

user2068060
  • 159
  • 1
  • 7
  • Is it o.k. if it calls a function in another thread? If it is, take a look at [timer queues](http://msdn.microsoft.com/en-us/library/windows/desktop/ms687003(v=vs.85).aspx). – Anton Kovalenko Feb 15 '13 at 17:30
  • I'm not sure, but It needs to update variables. I'm still learning about threads, but I am guessing that wouldn't work. – user2068060 Feb 15 '13 at 17:56

1 Answers1

2

Use Waitable Timers - SetWaitableTimer after calling CreateWaitableTimer

When the due time arrives, the timer is signaled and the thread that set the timer calls the optional completion routine.

There is an example of what I think is your desired usage pattern here.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • I think that is a busy wait, same as if I ran a delay() type of function. - just more precise. – user2068060 Feb 16 '13 at 11:25
  • 1
    No, that's not a busy wait. `SleepEx` puts the thread to sleep; the kernel will wake it only when an APC arrives. See http://msdn.microsoft.com/en-us/library/windows/desktop/ms686307%28v=vs.85%29.aspx – Harry Johnston Feb 18 '13 at 09:37