Time Approach Algorithm?
Can anyone shed a little ligh开发者_如何学Got on how I would go about creating an approach algorithm for a target time; having sleep(x), where x is initially large and decreases as the target time approaches?
It depends on your constraints, but an easy solution is to always divide the remaining time by 2 and then sleep for that time. It has logarithmic complexity and that is fine.
Often the OS only guarantees a granularity of 10 ms, so stop sleeping when time falls below 20 ms.
void sleep(unsigned long howLong, unsigned decrement)
{
if (howLong == 0)
return;
if (decrement < 2)
{
Sleep(howLong);
return;
}
unsigned long delay = howLong / decrement;
while (delay)
{
Sleep(delay);
delay /= decrement;
}
}
精彩评论