My OS is Windows 7 32-bit, and I'm using Visual Studio 2008.
In my C# program I use function Thread.Sleep(5) to cause my thread to sleep in 5(ms), and then I print the elapsed time to the screen. My code snippet:
Code:
for (int i = 0; i < 20; i++)
{
myTimer.Start();
Thread.Sleep(5);
myTimer.Stop();
dTime = myTimer.Duration;
Console.WriteLine("{0}\n", dTime);
// Thread.Sleep(1000);
}
Thread.Sleep(20000);
The strange thing is that: Depending on whether or not I comment the line Thread.Sleep(1000) (as you can see from the above code snippet), the Thread.Sleep(5) function may not work accurately. If I uncomment the line, then the function seems to work more accurately than when I comment it. Please have a look at the two pictures I attached herewith to see the results more clearly.
So, my question is what causes the inaccuracy in the second case? Is there any thing I can do to improve the accuracy in that case?
You can't really expect these things to be absolutely precise on a non RTOS. Do you really think there is a problem with the accuracy you see here? What are you actually trying to accomplish? The code doesn't look like it is doing anything meaningful.
Arjay is correct, deep down on the inside the timer send a WM_TIMER message to your WndProc, and if your thread is blocked then it can not process the message in a timely manner.
--------------------------------------------------------------------------------------------------------------------------
Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.
The timer I used to measure the elapsed time is a high performance timer with an accuracy in the microseconds scale. Its source code can be downloaded from the following address:
So I believe that the results of the measurements are correct.
@BigEd781: This code just does a simple task: start a timer, sleep the thread in 5 (ms) and then stop the timer and then get the elapsed time. Its sole purpose is to measure the period of time during which the thread really 'sleeps', in order to see whether or not Thread.Sleep(5) is accurate.
It doesn't seem that the thread is blocked in the second case, because the sleep time is smaller than 5 (ms). Do you have any idea about this?
Regards,
Kakashi
Last edited by hatake_kakashi; July 16th, 2010 at 11:48 PM.
Yes, Sleep isn't that accurate. When the OS thread scheduler runs, it hands out time slices to threads based on priority. It runs realtime, high, normal, below normal threads all in that order. If any realtime threads need to run, it runs them to completion, then it runs the high priority threads and so on.
When you call Sleep, the thread scheduler knows to skip over that thread until the sleep expires.
Now given how the thread scheduler works with regard to priority, it doesn't mean that just because the sleep has expired on your thread, it will immediately get to run.
Raising your thread priority to High may help, but you would still be sharing time slices with other high pri threads, so there wouldn't be any guarantee that your results would be consistant.
Sleep is accurate to whatever the system clocks accuracy is. Unless there's a bug in the implementation you are guaranteed that your thread will sleep *at the minimum* the time you specify. However it may sleep a bit longer if your system is busy processing other threads.
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Sleep is accurate to whatever the system clocks accuracy is. Unless there's a bug in the implementation you are guaranteed that your thread will sleep *at the minimum* the time you specify. However it may sleep a bit longer if your system is busy processing other threads.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.