5

I have a small problem regarding threading in C#. For some reason, my thread speeds up from 32ms delay to 16ms delay when I open Chrome, when I close Chrome it goes back to 32ms. I'm using Thread.Sleep(1000 / 60) for the delay. Can somebody explain why this is happening, and maybe suggest a possible solution?

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading;

 namespace ConsoleApplication2
 {
     class Program
     {
         static bool alive;
         static Thread thread;
         static DateTime last;

         static void Main(string[] args)
         {
             alive = true;
             thread = new Thread(new ThreadStart(Loop));
             thread.Start();

             Console.ReadKey();
         }

         static void Loop()
         {
             last = DateTime.Now;

             while (alive)
             {
                 DateTime current = DateTime.Now;
                 TimeSpan span = current - last;
                 last = current;

                 Console.WriteLine("{0}ms", span.Milliseconds);
                 Thread.Sleep(1000 / 60);
             }
         }
     }
 }
Er Suman G
  • 581
  • 5
  • 12
  • So just to confirm, while Chrome is running on your system, this thread on your console app runs slower - or at least sleeps for twice as long? Even though they shouldn't be interacting with each other at all? – Adam Plocher Oct 16 '13 at 22:38
  • Sample code please - how exactly you've implemented this will impact the results... – Basic Oct 16 '13 at 22:38
  • He posted the sample code... – Adam Plocher Oct 16 '13 at 22:39
  • 1
    don't do diagnostic testing with `DateTime`. Use `Stopwatch` instead. – jltrem Oct 16 '13 at 22:39
  • 1
    @Adam Plocher The thread is twice as fast while Chrome is running, it slows down immediatly when i close it. – Rob Hendriks Oct 16 '13 at 22:40
  • 1
    @jltrem I have the exact same problem when i use StopWatch. – Rob Hendriks Oct 16 '13 at 22:43
  • I've just been able to exactly replicate the problem, when you close Chrome it takes longer, but I must ask how you found this?! – JMK Oct 16 '13 at 22:48
  • 6
    There's no problem to solve here. Sleep means "sleep for at least this amount of time" and that's what its doing. When you put a thread to sleep it might *never* wake up again; there could be higher-priority threads as far as the eye can see. When you say Sleep the thread scheduler has broad latitude to do whatever it likes, and apparently when Chrome is running it likes to do something different. Why? Who can say? The scheduler is behaving as it is documented to behave - it is sleeping for at least 16 ms - so don't worry about it. – Eric Lippert Oct 16 '13 at 22:58
  • 1
    Here is a hypothesis that is let me emphasize a total guess. If you have an application that is mostly sleeping then the OS puts the CPU in a power-saving mode, and sleeps for an extra tick to keep the processor in that low power mode to preserve battery and/or keep temperature down. When you start Chrome the scheduler realizes that there is a huge memory-intensive GUI application running, pops out of low-power mode and maybe even brings more CPUs online. Suddenly you stop sleeping for as long. Is that what the OS is doing? Maybe. Just a guess. **But that kind of cleverness is allowed.** – Eric Lippert Oct 16 '13 at 23:03
  • 4
    If you want to know what is actually going on rather than my random speculations then my recommendation to you is that you begin by reading the eighty page long description of the thread scheduling algorithm in Windows Internals 6th Edition if this subject interests you. – Eric Lippert Oct 16 '13 at 23:06
  • Does this also happen when Windows Media Player is running by any chance? Some programs call the Windows Multimedia API function [timeBeginPeriod()](http://msdn.microsoft.com/en-us/library/windows/desktop/dd757624%28v=vs.85%29.aspx) which on some hardware and versions of Windows can change the resolution that you see with Thread.Sleep() See here, for example: http://stackoverflow.com/questions/7614936/can-i-improve-the-resolution-of-thread-sleep – Matthew Watson Oct 16 '13 at 23:07
  • 4
    More generally, **calling Sleep with any argument other than zero is probably a bug**. Why would you go to the expense of creating an entire thread only to put it to sleep? You wouldn't hire a worker and pay them to sleep. Threads are expensive; if you're not going to use one, put it back in the pool. – Eric Lippert Oct 16 '13 at 23:10
  • 1
    @EricLippert I sense a new blog post coming along! – JMK Oct 16 '13 at 23:19

5 Answers5

12

Just a post to confirm Matthew's correct answer. The accuracy of Thread.Sleep() is affected by the clock interrupt rate on Windows. It by default ticks 64 times per second, once every 15.625 msec. A Sleep() can only complete when such an interrupt occurs. The mental image here is the one induced by the word "sleep", the processor is in fact asleep and not executing code. Only that clock interrupt is going to wake it up again to resume executing your code.

Your choice of 1000/60 was a very unhappy one, that asks for 16 msec. Just a bit over 15.625 so you'll always wake back up at least 2 ticks later: 2 x 15.625 = 31 msec. What you measured.

That interrupt rate is however not fixed, it can be altered by a program. It does so by calling CreateTimerQueueTimer() or the legacy timeBeginPeriod(). A browser in general has a need to do so. Something simple as animating a GIF requires a better timer since GIF frame times are specified with a unit of 10 msec. Or in general any multi-media related operation needs it.

A very ugly side-effect of a program doing this is that this increased clock interrupt rate has system-wide effects. Like it did in your program. Your timer suddenly got accurate and you actually got the sleep duration you asked for, 16 msec. So Chrome is changing the rate to, probably, 1000 ticks per second. The maximum supported. And good for business when you have a competing operating system.

You can avoid this problem by picking a sleep duration that's a closer match to the default interrupt rate. If you ask for 15 then you'll get 15.625 and Chrome cannot have an effect on that. 31 is the next sweet spot. Etcetera, integer multiples of 15.625 and rounded down.

UPDATE: do note that this behavior changed just recently. Starting at Win10 version 2004, the effect is no longer global so Chrome can no longer affect your program. Starting at Win11, an app with an inactive window operates with the default interrupt rate.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
4

This is possibly occurring because Chrome (or some component of Chrome) is calling timeBeginPeriod() with a value that increases the resolution of the Windows API function Sleep(), which is called from Thread.Sleep().

See this thread for more information: Can I improve the resolution of Thread.Sleep?

I noticed this behavior with Windows Media Player some years ago: The behavior of one of our applications changed depending on whether Windows Media Player was running or not. It turned out, WMP was calling timeBeginPeriod().

However, in general, Thread.Sleep() (and by extension, the Windows API Sleep()) is extremely inaccurate.

wp78de
  • 18,207
  • 7
  • 43
  • 71
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
3

First, 1000 / 60 = 16 ms

The PC clock has a resolution of around 18-20ms, Sleep() and the result of DateTime.Now will be rounded to a multiple of that value.

So, Thread.Sleep(5) and Thread.Sleep(15) will delay for the same amount of time. And that can be 20, 40 or even 60 ms. You do not get much guarantees, the argument for Sleep() is only a minimum.

And another process (Chrome) that hogs the CPU (even a little) can influence the behavior of your program that way. Edit: that is the reverse of what you're seeing, so something a little else is happening here. Still, it's about rounding to timeslices.

H H
  • 263,252
  • 30
  • 330
  • 514
  • The issue here is that Chrome un-hogs the machine, it made the timer *better*. So clearly hogging doesn't explain anything. – Hans Passant Oct 18 '13 at 00:00
2

Basically, Thread.Sleep isn't very accurate.

Thread.Sleep(1000/60) (which evaluates to Thread.Sleep(16)), asks the thread to go to sleep and come back when 16ms has elapsed. However, that thread might not get to execute again until a greater amount of time has elapsed; say, for example, 32ms.

As for why Chrome is having an effect, I don't know but since Chrome spawns one new thread for each tab, it'll have an effect on the system's threading behaviour.

Community
  • 1
  • 1
Jeremy
  • 2,642
  • 18
  • 36
1

You are hitting a resolution issue with DateTime. You should use Stopwatch for this kind of precision. Eric Lippert states that DateTime is only accurate to around 30 ms, so your readings with it in this case will not tell you anything.

Measurement is half of your problem. The actual time variation for your loop is due to Sleep resolution (as stated in the other answers).

Community
  • 1
  • 1
jltrem
  • 12,124
  • 4
  • 40
  • 50
  • 1
    I've tried Stopwatch, it gave me the same output as DateTime. – Rob Hendriks Oct 16 '13 at 22:50
  • 2
    DateTime and the thread timer have basically the same resolution -- which should not be surprising -- so using one to time the other is a really bad idea. It's like trying to use an unmarked yardstick to measure the length of a second unmarked yardstick. Hard to do accurately. – Eric Lippert Oct 16 '13 at 22:54