For a bit of context, I am writing a simple CPU emulator. The emulator process boils down to calling a 'step' function to read and execute the next operation in the program. Currently this is just done as fast as possible in a while
loop.
I would like the code to be cross-platform but (unfortunately) windows is the primary target.
I need to be able to execute my Emulator->step()
function at regular intervals in the range of 1,000Hz to 100,000Hz.
For a slower loop I would simply use
sleep()
but (on windows at least) it doesn't have the resolution for such a high frequency.I have also toyed with spinning a loop checking a Boost microsecond timer. Ignoring the inaccuracy of this method, it uses up real CPU time whilst it is meant to be 'idle'. I am running several emulated CPUs concurrently in threads so the
while
loop causes a noticeable impact on performance.
Surely there is a method of doing what I want to do with C++?