SpinLock & SpinWait
This lesson discusses the SpinLock and SpinWait classes in C#
SpinLock & SpinWait
In this section we'll discuss alternatives to blocking threads.
SpinLock
Imagine a situation where a thread attempts to acquire an appropriately protected resource but finds itself blocked. The blocked thread may get context switched by the CPU for another thread since the first thread is waiting to get unblocked. The context switching of threads may incur a higher overhead than if the first thread were to simply busy-wait and continuously poll the resource to become available. C# provides a class SpinLock
which makes a thread wait in a loop repeatedly checking until the lock becomes available. The lock acquired implements mutual exclusion and is non-reentrant. Remember, SpinLock
is a value type and if passed around, must be done ...