-6

I am a bit puzzled. According to this thread the sleep function should "...block the current thread for at least...", implying a minimum sleep time.

However, a short test program implies that it sleeps less than 200ms. It tells me a call is made 6 times within 1199ms, which would mean a consistent shorter sleep time than 200ms.

The sample code that I used:

    static int numCalls = 0;
    static Stopwatch swSamples = new Stopwatch();

    static void Main(string[] args) {

        swSamples.Start();
        for (int i = 0; i < 6; i++) {

            // wait
            Thread.Sleep(200);

            // make call
            numCalls++;

        }

        // display 
        Console.WriteLine("numCalls: " + numCalls + "   -   " + swSamples.ElapsedMilliseconds);
        Console.ReadLine();

    }

Please note that this question is not about getting exact 5 calls per second, it is about the time a thread actually sleeps not being "at least" ...ms

Maximoes
  • 1
  • 1
  • 2
    You increment `numCalls` before the first time you call `Thread.Sleep`, so your initial count is off by one. – BJ Myers Jul 23 '17 at 16:39
  • Thank you for the response. I think you misunderstood my question. The question was not about getting 5 or 6 calls per second. It was about how 6 loops can take 1199 ms when the sleep should be "at least" 200ms.. – Maximoes Jul 23 '17 at 19:39
  • Also, I am new to this, did I get downvotes on my question because people though it was about making 5 instead of 6 calls happen? Because that was not what this question is about... – Maximoes Jul 23 '17 at 19:40
  • No, I understand your question. You didn't go through 6 loops in 1199 ms, you only went through 5. Your code just prints 6 because the variable is incremented too early. – BJ Myers Jul 23 '17 at 22:26
  • That would mean every 200ms sleep takes 240ms, which is unlikely. I really think it is sleeping 6 times. I added another the sample code in the question to make it less confusing, still resulting in 1199ms with 6 sleep calls. – Maximoes Jul 24 '17 at 01:28
  • 1
    Yes, in your second example you do in fact call `Thread.Sleep(200)` 6 times. But not in 100 different trials could I even once reproduce your claim that the elapsed time was less than 1200 ms. And frankly, even if I could, if the best I could show was 1199 ms instead of 1200 ms, I'd chalk that up to a measurement error (e.g. some rounding in `Stopwatch`), rather than some genuine misbehavior of `Thread.Sleep()`. – Peter Duniho Jul 24 '17 at 06:01
  • Ok, the first example was indeed less ideal. I removed it from the question since it distracts from the main question. I am not asking to reproduce, I guess the question that I want an answer to is: Is it possible that Thread.Sleep() sleeps less than the given ms? And if so, why? – Maximoes Jul 24 '17 at 10:18
  • I only recently found this question (https://stackoverflow.com/questions/11376307/when-does-thread-sleep1000-sleeps-less-than-1000-milliseconds?rq=1). Which is kind of what I am dealing with, so does this effect also exist in c#? – Maximoes Jul 24 '17 at 10:22

1 Answers1

1

Putting a thread to sleep tells the thread to yield to the system for a given time. This time is only as accurate as the hardware clocks and the time resolution of the system. Hardware clocks are minutely susceptible to temperature and voltage variations and if the resolution of the clock is less than the resolution of your request, this could result in an early or late resume of thread operation.

Setherith
  • 329
  • 2
  • 10