Are there any examples in the .Net framework that use spinlock or spinwait?
Pretty much any of the low-lock concurrent collection class will likely use some combination of SpinWait
and Yield
. Though ConcurrentDictionary
is one notable exception. The list of classes I found include the following.
- ManualResetEventSlim
- SemaphoreSlim
- SpinLock
- Barrier
- ReaderWriterLockSlim
- ConcurrentQueue
- ConcurrentStack
- BlockingCollection
What do you mean by normal locking ? Some kind of lock(object) { ... }
construct ?
If yes, you should look at ConcurrentStack
for example, it uses a SpinWait
for its job.
ConcurrentStack and ConcurrentQueue are lock-free collections, however ConcurrentDictionary, ConcurrentBag are using locks. BlockingCollections is just a container so it is not a collection by itself, it has to wrap other threadsafe collections, for examplevar bc = new BlockingCollection(new ConcurrentStack());
this is an introductory article about how to use SpinWait in lock-free codehttp://www.emadomara.com/2011/08/spinwait-and-lock-free-code.html