Giving up time slice

Giving up time slice

Post by nutso fass » Sat, 08 Oct 2005 02:36:34


Sleep(0) "causes a thread to relinquish the remainder of its time slice to
any thread of equal priority that is ready to run."

Assuming there is another thread of equal priority ready to run, and the
first calls Sleep(0), does the next thread receive the remainder of the
first's time slice PLUS its previously scheduled slice, or does the
scheduler increment immediately, giving the next thread a SHORTER slice than
normal?

thx, nf
 
 
 

Giving up time slice

Post by Jochen Kal » Sat, 08 Oct 2005 03:18:31

Hi nutso!

It will give the next thread the remaining time-slice (and therefor a
shorter time-slice).
And as a side-effect the thread-times of the first thread will *not*
increase (can be queried via GetThreadTimes). All the thread-time is
added to the second thread.

With this is mind you can create thread will can use many CPU-power
without ever increasing the Thread-Time of this thread :-) (but you
need at least one other thread which always is ready to run!).

--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/

 
 
 

Giving up time slice

Post by Jochen Kal » Sat, 08 Oct 2005 03:33:24


For more info see: http://www.yqcomputer.com/

--
Greetings
Jochen

My blog about Win32 and .NET
http://www.yqcomputer.com/
 
 
 

Giving up time slice

Post by nutso fass » Sat, 08 Oct 2005 08:28:31

Hi, Jochen.

That's what I suspected. The GetThreadTimes result is a surprise.

Thanks much!

nf
 
 
 

Giving up time slice

Post by Skywin » Sat, 08 Oct 2005 09:37:12

Note that it won't necessarily yield to another thread if there are no (>=
priority) threads ready to run.

There are other reasons why Sleep(0) is often not a good idea;
http://www.yqcomputer.com/
overview of several of them.
 
 
 

Giving up time slice

Post by nutso fass » Sat, 08 Oct 2005 15:40:29


So, if you want to give up the remainder of a timeslice and don't have a
specific thread you want to give it to, use Sleep(1), not Sleep(0).

thx & rgds, nf
 
 
 

Giving up time slice

Post by Eugene Ger » Sat, 08 Oct 2005 17:22:57

 
 
 

Giving up time slice

Post by Skywin » Sun, 09 Oct 2005 00:44:53

If at all possible I would wait on a real synchronization object...
 
 
 

Giving up time slice

Post by Arnaud Deb » Sun, 09 Oct 2005 19:30:27


This question is a recurrent in the group, but I was unable to figure until
now a situation where it may be usefull to call either Sleep(0) either
Sleep(1). If you've got something to do, do it immediately and the scheduler
will take care of being fair among all running threads. If you don't have
anything to do, just wait on a blocking call until you're notified you've
got work. In what situation may it be of any important to yield the restr of
one timeslice? Even when writnig non-blocking multithreading code
(spinning), I do not see Sleep(0) being of any help.

Arnaud
MVP - VC
 
 
 

Giving up time slice

Post by nutso fass » Mon, 10 Oct 2005 12:03:51

Arnaud,
of

Sleep(0) could be used to limit the %_of_possible_work a specific thread can
do in increments less than the minimum timer resolution, admittedly an
unlikely need.

Not exactly relevant to your question, but I noticed that in spawning a
thread that does critical timing, be it with Sleep or a waitable timer, the
results were gibberish unless I put the thread into at least a 100ms Sleep
before doing any useful work.

rgds, nf