The behavior of sleep is already configurable but I guess you are suggesting that the delta time is greater than half the update rate.
I suppose it’s a bit arbitrary but when update rate is 60 hz, the sleep() resolution on many OS’s will be at or greater than a 60 hz frame (or maybe that’s legacy vestigial lore on my part, it could be 10ms or 13ms ‘back in the day’). Definitely more than half a frame in that case.
PLUS, the delta is measured as actual loop to loop time… so if we skipped the sleep and the actual frame time is still more than half the desired time, there is no way we’d make it to the next frame on time if we slept.
sleep(0) is a special case that should only return immediately if there are no other higher priority threads waiting. So it may LOOK like your CPU is pegged but the CPU is still available to do other things.
sleep(1) is guaranteed to release the CPU frame… so you can set idleSleepTime to 1 and you will occasionally miss frames. (If the case you indicate is true then you will sleep every other frame and run every other frame a little late.)
The case I’m trying to prevent is getting underflow when CPU is 0… which is what was happening all. the. time. until I added this code.
It’s possible that I should swap this all out with LockSupport.parkNanos() which is supposedly more accurate than sleep.
It’s also possible that this should be a strategy object that the user can override.