Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / September 2003

Tip: Looking for answers? Try searching our database.

Can someone explain Thread.SpinWait() method?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Varn - 22 Sep 2003 22:59 GMT
I was looking at the docs for Thread and can't understand the difference
between Thread.Sleep and Thread .SpinWait.  What exactly does SpinWait do?
The MSDN docs are very general on this one.

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
varnk@diebold.com
-----------------------------------

Richard A. Lowe - 22 Sep 2003 23:16 GMT
I can quote Juval Lowy:
"When a thread calls SpinWait(), the calling thread waits
the number of iterations specified, and the thread is
never added to the queue of waiting threads. As a result,
the thread is effectively put to sleep without
relinquishing the remainder of its CPU time slot.

The .NET documentation does not define what an iteration
is, but it is likely mapped to a predetermined number
(probably just one) of NOP (no-operations) assembly
instructions. Consequently, the following SpinWait()
instruction will take different time to complete on
machines with different CPU clock speeds:

const long MILLION = 1000000;
Thread.SpinWait(MILLION);

SpinWait() is not intended to replace Sleep(), but is
rather made available as an advanced optimization
technique. If you know that some resource your thread is
waiting for will become available in the immediate future,
it is potentially more efficient to spin and wait, instead
of using either Sleep() or a synchronization object,
because those force a thread context switch, which is one
of the most expensive operations performed by the
operating system. Even in the esoteric cases for which
SpinWait() was designed, using it is an educated guess at
best. SpinWait() will gain you nothing if the resource is
not available at the end of the call, or if the operating
system preempts your thread because its time slot has
elapsed, or because another thread with a higher priority
is ready to run. In general, I recommend that you should
always use deterministic programming (using
synchronization objects in this case) and avoid ptimization
techniques."

HTH,
Richard

>-----Original Message-----
>I was looking at the docs for Thread and can't understand the difference
>between Thread.Sleep and Thread .SpinWait.  What exactly does SpinWait do?
>The MSDN docs are very general on this one.
Nick Wienholt - 22 Sep 2003 23:20 GMT
SpinWait keeps the calling thread active on the processor, while Sleep
results in the calling thread by preemted off the processor.  If you
expected a resource that a thread is waiting on to become available very
soon (say, for example, that another processor on the same box will be
releasing a lock on it), you could SpinWait for a certain period, then try
to acquire the resource again before your processor timeslice expired.
SpinWait is an optimization technique to avoid excessive context switching.

Nick Wienholt, MVP
Maximizing .NET Performance
http://www.apress.com/book/bookDisplay.html?bID=217
Sydney Deep .NET User Group www.sdnug.org

> I was looking at the docs for Thread and can't understand the difference
> between Thread.Sleep and Thread .SpinWait.  What exactly does SpinWait do?
> The MSDN docs are very general on this one.
Phil Wilson - 23 Sep 2003 01:28 GMT
That ...."another processor..." is a key point. On a single processor box you're
likely to take processor time away from the thread that's got the resource you
want, so it can't run to release it anyway. Then if that other thread doesn't
run until your quantum expires, you're doing context switches anyway.
Signature

Phil Wilson [MVP Windows Installer]
----

> SpinWait keeps the calling thread active on the processor, while Sleep
> results in the calling thread by preemted off the processor.  If you
[quoted text clipped - 12 lines]
> > between Thread.Sleep and Thread .SpinWait.  What exactly does SpinWait do?
> > The MSDN docs are very general on this one.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.