Are there any examples in the .Net framework that use spinlock or spinwait?
I had a look at the concurrent col开发者_运维知识库lections but they appear to use normal locking underneath the hood. Are there any good examples in the .Net framework that use this locking construct?
I initially looked at ConcurrentDictionary. I saw it was using normal locks but the BlockingCollection class uses SpinWait in its internal methods.
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 example var bc = new BlockingCollection(new ConcurrentStack());
this is an introductory article about how to use SpinWait in lock-free code http://www.emadomara.com/2011/08/spinwait-and-lock-free-code.html
精彩评论