Background: I have written a gameboy emulator in F#/.net. In the gameboy, the vblank (~fps) is ~60 Hz and is tied to the game speed, it's therefore important that the emulator runs as close to 60 fps as possible.
Since a modern computer can run my emulator a lot faster than 60 fps I need to slow down the emulator. My current approach for this is to calculate the time between two VBlanks and then wait the remaining amount of time for that vblank period.
The problem comes with how to wait without busy looping the cpu. Since I typically need to wait a couple of ms (sometimes more, sometimes less), the built in Thread.sleep function is not a good choice, since unless you specify 0 wait time, it will sleep at least ~15 ms, which is way to long (and inaccurate). My current approach is to use sleep(0) which really is just a fancy spinlock (where other threads may run, but you still max out the cpu).
What's the proper way to solve this? I was thinking about waiting for a semaphore that's released from a timer, but can a timer provide the time resolution needed? And isn't that just a fancy sleep anyway?
Edit: This was tagged as duplicate of What Thread sleep method is most precise: Monitor.Wait vs System.Timer vs DispatchTimer vs Threading.Timer but I think this is less question of precision, and more to find an appropiate solution for a tight game loop.