Thread.Sleep(0)

F

Frankie

This is from MSDN online
(http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx):
"Specify zero (0) to indicate that this thread should be suspended to allow
other waiting threads to execute."

My questions:

1. By "other threads" - does the above statement refer specifically and only
to other threads in the current AppDomain?

2. When would execution continue in the current thread (after a thread hits
Thread.Sleep(0))? Is it after all other threads - regardless of their
priority - have run their course completely, or is it after only higher
priority threads have run their course?

Just looking to understand the implications of adding Thread.Sleep(0) to my
code - and when doing so would be a good thing [or possibly bad thing] to
do.

Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

Frankie,

1. By other threads, it does not mean other threads in the app domain. It
means any other thread that is waiting that is of equal priority (see
http://msdn2.microsoft.com/en-us/library/ms686307.aspx for more details, as
SleepEx is what is ultimately called).

2. It's not guaranteed when your thread will regain control. That's up to
the scheduler on the OS to decide.

Why is it that you want to use Sleep(0)?
 
F

Frankie

Thanks Nicholas... RE:
<< Why is it that you want to use Sleep(0)? >>

I saw it used in sample code while researching the AsyncOperation.Post
method (at
http://msdn2.microsoft.com/en-us/library/system.componentmodel.asyncoperation.post.aspx).

The sample code at that link includes the following comment and call to
Thread.Sleep:
// Yield the rest of this time slice.
Thread.Sleep(0);

It makes sense to me, in this context, to have a thread .Sleep(0) when it's
done doing its "important" and potentially processor-intensive work... so
that other threads - presumably initiated by the current app's
other/concurrent async operations - can do their "important" work. then come
back later to do cleanup when the thread schedule gets around to it. But all
of this rationalle I just stated is my best guess as to why Thread.Sleep(0)
was put in there by the authors of the sample code.

Frankie




Nicholas Paldino said:
Frankie,

1. By other threads, it does not mean other threads in the app domain. It
means any other thread that is waiting that is of equal priority (see
http://msdn2.microsoft.com/en-us/library/ms686307.aspx for more details,
as SleepEx is what is ultimately called).

2. It's not guaranteed when your thread will regain control. That's up
to the scheduler on the OS to decide.

Why is it that you want to use Sleep(0)?

<snip>
 
W

Willy Denoyette [MVP]

Frankie said:
This is from MSDN online
(http://msdn2.microsoft.com/en-us/library/d00bd51t.aspx):
"Specify zero (0) to indicate that this thread should be suspended to
allow other waiting threads to execute."

My questions:

1. By "other threads" - does the above statement refer specifically and
only to other threads in the current AppDomain?

2. When would execution continue in the current thread (after a thread
hits Thread.Sleep(0))? Is it after all other threads - regardless of their
priority - have run their course completely, or is it after only higher
priority threads have run their course?

Just looking to understand the implications of adding Thread.Sleep(0) to
my code - and when doing so would be a good thing [or possibly bad thing]
to do.

Thanks.

Be careful with Thread.Sleep(0), read the Community Content at the bottom of
the above msdn page.
There is IMO no good reason to call this API with a 0 msec. time-out,
especially not when called in a tight loop.

Willy.
 
N

Nicholas Paldino [.NET/C# MVP]

Frankie,

To be honest, I think that it shouldn't be in the sample code, as I
would consider that a premature optimization. Generally, you are using
threads already to handle the processing of other units of work
concurrently. If you want to give up your processing time slice in general,
then you should make a call to another asynchronous method.

I think that Sleep(0) is one of those things that should be used after
you have finished the code, and through profiling you realize that you are
hogging up too much processor time.
 

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