uriDium
uriDium

Reputation: 13420

Are there any examples in the .Net framework that use spinlock or spinwait?

I had a look at the concurrent collections 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.

Upvotes: 10

Views: 601

Answers (3)

Emad Omara
Emad Omara

Reputation: 532

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

Upvotes: 2

Brian Gideon
Brian Gideon

Reputation: 48949

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

Upvotes: 5

user703016
user703016

Reputation: 37945

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.

Upvotes: 4

Related Questions