ThreadSleep of Zero

  • Thread starter Thread starter Bob Day
  • Start date Start date
B

Bob Day

Using VS 2003, VB.net, MSDE...

I have seen some sample programs use the following line to yield a time
slice to other threads:
Thread.CurrentThread.Sleep(0)

The line does not produce a error, but I don't follow why it would be needed
or when it would be needed.

Please advise.

Bob Day
 
Using VS 2003, VB.net, MSDE...

I have seen some sample programs use the following line to yield a time
slice to other threads:
Thread.CurrentThread.Sleep(0)

The line does not produce a error, but I don't follow why it would be needed
or when it would be needed.

Please advise.

Bob Day

It's in the docs... A value of 0 tells the OS to suspend the thread in
favor of other waiting threads. Basically, you're saying to the OS, I'm
done for now, give someone else the rest of my time-slice.
 
Yes, I agree and understand that. But wouldn't that happen automatically
anyway? In other words, should you put.Sleep(0) through out various threads
to allow other threads to run? I know the anwser to that is no, so if it
happens automatically, what is the point of putting it in manually as
..Sleep(0)?

Thanks!
Bob Day
 
Yes, I agree and understand that. But wouldn't that happen automatically
anyway? In other words, should you put.Sleep(0) through out various threads
to allow other threads to run? I know the anwser to that is no, so if it
happens automatically, what is the point of putting it in manually as
.Sleep(0)?

Thanks!
Bob Day

The answer is no. The os will switch tasks when the specified timeslice is
over. The Sleep(0) is just a way of giving up the rest of your timeslice
if you know you aren't going to need it. I imagine that it might give you
a bit of a performance boost in certain situations... I suppose it would
depend on how much and how often you gave up the rest of your time :)
 
Bob,
In addition to the other comments.

Remember when windows schedules your thread it looks at 2 main items:
Timeslice & Priority.

If your thread is a higher priority then other threads it will get most of
the timeslices. Thread.Sleep(0) gives lower priority threads a quicker
chance to run. (Windows does temporarily adjust priorities so all the lower
priority threads are not totally starved).

A Timeslice is some fraction of a second (lets say 100 ms) that windows
allows your thread to run. If each loop iteration in your thread takes only
10 ms, you can call Thread.Sleep(0) to give 90 ms (the reminder of your
timeslice) to the next thread that is available to run. Without the
Thread.Sleep(0) your thread would process 10 iterations before relinquishing
control. Depending on what you are doing this may cause an overall
sluggishness to the computer.

Sleep(0) is also useful if you are polling a resource, such as a shared
field where one thread sets it and a second thread reads it. With Sleep(0)
the second thread can give up its timeslice to the first thread, allowing
the first thread to finish calculating & setting the value. Rather then be
in a "tight" loop checking to see if the field changed. However! I would
normally use a ManualResetEvent or AutoResetEvent instead of Sleep(0), as
they are friendlier.

Hope this helps
Jay
 

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

Back
Top