.Net and thread.Sleep accuracy

P

Pawan Singh

Hi,

I need a way for my thread to wake up at a specified time in .Net. I know
that windows is not a real time operating system, so I have to live with
much less precise timing. But what I am finding is that .Net thread.sleep is
inaccurate by 600 milliseconds some times. Most of the time, it is within 25
milliseconds which I am assuming is the Windows kernel scheduler quantum on
my XP machine. Since the problem happens with "Thread.Sleep" or
"Socket.Select" or any WaitOne method of events, I am assuming that is
either underlying Windows O/S issue or .Net framework issue. I have never
seen Win32 code miss the timing by such huge margin even when the CPU is
100% in use by some other process. But it happens all the time if CPU is
buys in .Net framework. So I am assuming that whatever layer which abstracts
Win32 threads and sleeps has some problems with it. Anyone has any
explanations?

Thanks
Pawan

Here is the piece of code for elucidation:

Dim start as Date = Now
thread.sleep(200) ' sleep for 200 milliseconds (you can put " sock.Select or
sock.Poll or WaitOne on some event here with valid timespans and result is
same)
Dim end as Date = Now
Dim diff as Timespan = end.Subtract(start)

' diff here is for the most part within 25 milliseconds. But sometimes, it
is off by 600 milliseconds or more even if the current thread's priority is
set to "highest".
' I can live with 25-50 milliseconds (even 100) but not 600 milliseconds.
That just seems incorrect on modern O/Ss with 3 Gigahertz CPUs)
 
P

Pawan Singh

I think I have found the problem. But I am still looking for solution. The
problem is following:

the process has multiple threads which are cpu and memory intensive. This is
causing the garbage collector to hijack all the threads. Can I control the
garbage collector by telling it not to do touch any objects associated with
a particular thread and not suspend that thread?

Thanks
Pawan
 
R

Richard Grimes [MVP]

Pawan said:
I think I have found the problem. But I am still looking for
solution. The problem is following:

the process has multiple threads which are cpu and memory intensive.
This is causing the garbage collector to hijack all the threads. Can
I control the garbage collector by telling it not to do touch any
objects associated with a particular thread and not suspend that
thread?

If you pin an object the GC won't touch it (ie move it in memory). GCHandle
allows you to pin an object, but be aware that you'll need to unpin the
object at some point.

However, if your other threads are CPU intensive isn't your problem due to
thread starvation? (ie the other threads are getting the CPU and not your
awakening one?)

I guess another idea is to tell the GC to do a collection before you tell
your thread to sleep, if the thread sleeps for only a short amount of time
this would mean that another collection is unlikely to occur while the
thread is sleeping.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top