473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ThreadSleep of Zero

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
Nov 20 '05 #1
5 1972
In article <OP**************@TK2MSFTNGP11.phx.gbl>, Bob Day wrote:
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.

--
Tom Shelton [MVP]
Nov 21 '05 #2
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

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
In article <OP**************@TK2MSFTNGP11.phx.gbl>, Bob Day wrote:
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.

--
Tom Shelton [MVP]

Nov 21 '05 #3
On Mon, 16 Aug 2004 21:01:52 -0500, Bob Day wrote:
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 :)

--
Tom Shelton [MVP]
Nov 21 '05 #4
Bob,

Did you look at the thread priority already?

Cor
Nov 21 '05 #5
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
"Bob Day" <Bo****@TouchTalk.net> wrote in message
news:ur**************@TK2MSFTNGP10.phx.gbl...
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

"Tom Shelton" <to*@YOUKNOWTHEDRILLmtogden.com> wrote in message
news:uw**************@TK2MSFTNGP12.phx.gbl...
In article <OP**************@TK2MSFTNGP11.phx.gbl>, Bob Day wrote:
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.

--
Tom Shelton [MVP]


Nov 21 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
28
by: Andreas Prilop | last post by:
Jukka reports on http://www.cs.tut.fi/~jkorpela/chars/spaces.html that Internet Explorer 6 fails on the "zero width space" U+200B ​ Is this observation still valid? For which versions of MS...
15
by: I wish | last post by:
#include <string.h> int a; memset( a, 0, sizeof(a) ); Does that guarantee all bits zero? -- |
25
by: Mantorok Redgormor | last post by:
Finally, control is returned to the host environment. If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned. beyond this...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
25
by: pm940 | last post by:
Hello. I've been reading some past discussions on the NULL vs. zero. References are always made to systems or machienes that use values other than zero to represent the NULL pointer. Although...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
15
by: Tomás | last post by:
Is the following fully legal and fully portable for all the unsigned types? The aim of the function is to take an array by reference and set each element's value to zero. #include <... ...
4
by: H.S. | last post by:
Hello, I am trying out a few methods with which to test of a given number is practically zero. as an example, does the following test correctly if a given number is zero within machine...
23
by: Hallvard B Furuseth | last post by:
As far as I can tell, (x & -1) is nonzero if the integer x is negative zero. So for signed types, x == 0 does not guarantee (x & foo) == 0. Is that right? (Not that I expect to ever encounter a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.