I'm writing an emulator for a system with C++, using Qt for UI, event handling, and so on. Obviously, I need to have an accurate timing reference. Right now, I use a QTimer with the appropriate interval (16.66667 ms for NTSC, 20 ms for PAL) but as expected, the accuracy of that is horrible. Sometimes the timer fires at 50 FPS, sometimes 120 FPS.
I realise that most operating systems have some function to provide an accurate timestamp down to the micro- (or nano-) second level. (i.e. QueryPerformanceCounter
on Windows.)
Instead of using the system clocks, I was also thinking of synchronising to the audio clock, since I already have an audio output stream, but I don't think Qt offers functionality for that.
What would be the easiest (and/or best) way to synchronise to some accurate clock to call a function at a specific time interval, with little jitter during normal circumstances, from C++/Qt?