开发者

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;
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜