vote buttons
0
1
intermediate
0
beginner
0
advanced


19-Oct-2014 10:49 UTC
K Bonneau
350

1 Answers

vote buttons
0

A Thread is blocked when its execution is paused. When you call Sleep, Join, EndInvoke the thread is blocked. As threads receive time slices every often to execute work, the time slice offered to a blocked thread yields immediately i.e it gives up its time slice as it is just waiting. Although this seems efficient, it also unnecessarily received a time slice in a context switch which could hurt performance.

A spinning thread is just like an empty loop. If you use spinning (using Thread.SpinWait() in.NET) the thread would spin in an empty loop for sometime and keep using CPU unlike in the case of a blocked thread where the time slice is yielded. We can use SpinWait if we know the resource we are waiting for would be available in a very short span of time. i.e. if the time required to get the resource is less than the overhead of context switching you should use SpinWait. The .net implementation of SpinWait is such that it limits the CPU-intensive spinning to a few number of iterations, and thereafter it yields its time slice on every spin, to minimize misuse of the CPU. On single-core computers, SpinWait yields the time slice of the thread immediately because spinning blocks forward progress on all thread as there can only be one physical executing thread.

19-Oct-2014 10:49 UTC
K Bonneau
350
Related Questions