Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Sleep inconsistency?

Having a bit of an issue with a game I'm making using opengl. The game will sometimes run at half speed and sometimes it will run normally.

I don't think it is the opengl causing the problem since it runs at literally 14,000 fps on my computer. (even when its running at half speed)

This has led me to believe that is is the "game timer" thats causing the problem. The game timer runs on a seperate thread and is programmed to pause at the end of its "loop" with a Sleep(5) call. if i remove the Sleep(5) call, it runs so fast that i can barely see the sprites on the screen. (predictable behavior)

I tried throwing a Sleep(16) at the end of the Render() thread (also on its own thread). This action should limit the fps to around 62. Remember that the app runs sometimes at its intended speed and sometimes at half speed (i have tried on both of the computers that i own and it persists).

When it runs at its intended speed, the fps is 62 (good) and sometimes 31-ish (bad). it never switches between half speed and full speed mid execution, and the problem persists even after a reboot..

So its not the rendering that causing the slowness, its the Sleep() function

I guess what im saying is that the Sleep() function is inconsistent with the times that it actually sleeps. is this a proven thing? is there a better Sleep() function that i could use?

like image 642
user2045245 Avatar asked Mar 24 '23 19:03

user2045245


1 Answers

A waitable timer (CreateWaitableTimer and WaitForSingleObject or friends) is much better for periodic wakeup.

However, in your case you probably should just enable VSYNC.

like image 72
Ben Voigt Avatar answered Apr 06 '23 17:04

Ben Voigt