Waiting Till Next Frame (Spinning Issue)

Started by
2 comments, last by TheUnbeliever 12 years, 6 months ago
So, my server does a number of 'frames' a second and sends the updates to the clients but when it's not time for the next frame it simply spins...

This is kinda weak because it uses more processing power than is needed most of the time.
Is there any way to 'sleep' the process for a reliable amount of time? I hear Sleep is not reliable at all but I could be wrong.
Advertisement
On Windows, for accurate real-time systems like games, there isn't really a solution for this.

The most accurate thing you can do is to make a spin-loop that keeps calling [font="'Courier New"]Sleep(0)[/font] and/or [font="'Courier New"]YieldProcessor[/font], etc... Unfortunately, it's really a trade-off between accuracy and power efficiency :/
I figured as much, I'm currently calling YieldProcessor so I guess I'll just keep it at that.
Considerations for sleep(0)
[TheUnbeliever]
Sleep is abysmal. Stay away from it like from the plague.

The most reliable and accurate thing you can use under Windows is[font="Courier New"] CreateWaitableTimer [/font]and [font="Courier New"]SetWaitableTimer[/font][font="Arial"]. Be sure to use[/font] [font="Courier New"]timeBeginPeriod [/font]to have 1ms resolution[font="Arial"].
[/font][font="Arial"]This allows you to create a timer that fires regularly and use any of the wait functions on that handle to block at 1ms accuracy. This is much better than anything you could possibly get from Sleep.
[/font]

This topic is closed to new replies.

Advertisement