-3

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

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

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
DolbiComp
  • 1
  • 1
  • 1
    My guess is that on your home PC, you're running Chrome, which speeds up the timer to 1ms resolution. The server on the other hand is using the default 15ms resolution, which is better for server workloads. (You don't want your server constantly interrupted.) The behavior on the home PC is [likely to change soon](https://randomascii.wordpress.com/2020/10/04/windows-timer-resolution-the-great-rule-change/) so that only programs that requested the higher timer resolution will get it. – Raymond Chen Oct 24 '20 at 20:01
  • 2
    Does this answer your question? [WinAPI Sleep() function call sleeps for longer than expected](https://stackoverflow.com/questions/9518106/winapi-sleep-function-call-sleeps-for-longer-than-expected) – Raymond Chen Oct 24 '20 at 20:03

1 Answers1

0

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.

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

Source:

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.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69