Windows Sleep Incompatibility?

You have a problem with a game that I am making using opengl. The game will sometimes work at half speed, and sometimes it will work fine.

I donโ€™t think this is the problem causing the problem, since it runs at literally 14,000 frames per second on my computer. (even when it is running at half speed)

This led me to think that this is a โ€œgame timerโ€ that is causing the problem. The game timer starts on a separate thread and is programmed to pause at the end of the "loop" when calling "Sleep" (5). if I delete the Sleep (5) call, it works so fast that I can barely see the sprites on the screen. (predictable behavior)

I tried to reset Sleep (16) at the end of the Render () thread (also in my thread). This action should limit fps to about 62. Remember that the application runs at a certain speed, and sometimes at half speed (I tried on both computers that I have, and it persists).

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

So this is not a slow-rendering rendering, its Sleep () function

I assume that I am saying that the Sleep () function is incompatible with the time it really sleeps. is this a verified thing? is there a better Sleep () function that I could use?

+4
source share
4 answers

The expected timer ( CreateWaitableTimer and WaitForSingleObject or friends) is much better for periodically waking up.

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

+5
source

See the following discussion of the Sleep function, focusing on a bit about scheduling priorities: http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx

+1
source

yes, the sleep function is inconsistency, it is very useful in case of macrostate. if you want matching time, use QueryPerformanceFrequency to get the CPU frequency, and QueryPerformanceCount twice for the start and end, and then (final start) / frequency get the matching time, but you have to make sure that if your processor is a mulit kernel, time The beginnings and endings may not be the same CPU core, so please setThreadAffinity for your working thread set the same CPU core.

+1
source

There was the same problem. I just made my own sleep logic and worked for me.

 #include <chrono> using namespace std::chrono; high_resolution_clock::time_point sleep_start_time = high_resolution_clock::now(); while (duration_cast<duration<double>>(high_resolution_clock::now() - sleep_start_time).count() < must_sleep_duration) {} 
0
source

Source: https://habr.com/ru/post/1483510/


All Articles