Sleep()睡得太久 [英] Sleep() sleeps too long

查看:65
本文介绍了Sleep()睡得太久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows Server 2016/2019有问题在典型的循环中,用于"重复1000次,暂停时间为1毫秒.

There is a problem with Windows Server 2016/2019 In a typical cycle "for" 1000 repetitions, with a pause of 1 ms.

  for (int i = 0; i <1000; i ++)
  {
      Thread.Sleep (1);
  }

服务器处理每个睡眠周期-15毫秒.并自1000年以来.而不是1000毫秒.(1秒)执行15000 ms.(15秒)

The server processes each Sleep cycle - 15 ms. And since 1000 requests. Instead of 1000 ms. (1 sec) 15000 ms is executed. (15 sec)

在家用PC上可以.仅在服务器上存在此问题.我怀疑是CPU的罪魁祸首.

On the home PC is all right. Only on the server is this problem. I suspect that the CPU is to blame.

推荐答案

一般来说:仅保证在指定时间内睡眠至少 时间-不是确切时间.您将获得下一个用于处理的timslot-最简单的解决方案-不要旋转和睡眠-如果您想要的话,睡眠1000毫秒-这样您就可以接近1睡眠.通过循环运行1000次,您的稍大一点的睡眠"就可以总结.

Genrally: sleep only gurantees to sleep at minimum for the specified time - not the exact time. You get the next timseslot for processing - easiest solution - dont spin and sleep - sleep 1000ms if you want to have that - so you'll get closer to 1s sleeps. By looping 1000 times your "slightly bigger sleeps" sum up.

请参见此处的C#: sleep()-更一般的Windows睡眠在这里:

See f.e. for C# here: sleep() - and more generally for Windows sleep here: win32/api/synchapi/nf-synchapi-sleep.

来源:

经过睡眠间隔后,线程即可运行.如果指定0毫秒,则线程将放弃其时间片的其余部分,但保持就绪状态.请注意,不能保证就绪线程可以立即运行.因此,线程可能要等到睡眠间隔结束后的一段时间才能运行.有关更多信息,请参见安排优先级.

After the sleep interval has passed, the thread is ready to run. If you specify 0 milliseconds, the thread will relinquish the remainder of its time slice but remain ready. Note that a ready thread is not guaranteed to run immediately. Consequently, the thread may not run until some time after the sleep interval elapses. For more information, see Scheduling Priorities.

这篇关于Sleep()睡得太久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆