I am learning basics of parallel programming in .NET and I am wondering why someone would need to use SpinLock
sync primitive. I read that using of this mechanism avoids rescheduling when waiting for sync primitive release (and it worth it when waiting time is short), but is there any way to measure 'effectivity'? I don't want to make a decision whether to use SpinLock
or not only because someone said 'you shouldn't do that in most cases'.
Asked
Active
Viewed 107 times
0

Qué Padre
- 2,005
- 3
- 24
- 39
-
3Didn't [this](http://msdn.microsoft.com/en-us/library/dd460716.aspx) help? (second result when googling `SpinLock c#`) – I4V Jul 25 '13 at 12:14
-
It's quite hard to answer that question. But, I agree with : 'you shouldn't do that in most cases'. Even the .NET Framework seems to use it very rarely. – Cédric Bignon Jul 25 '13 at 12:24
-
I guess that's explained in this question: http://stackoverflow.com/questions/5869825/when-should-one-use-a-spinlock-instead-of-mutex – Anton Kattsyn Jul 25 '13 at 12:26
1 Answers
1
By Using SpinLock you actually telling the Thread to do a busy waiting - the thread is not going in to a blocked state, spinning wastes CPU cycles and prevents the CPU from doing more useful work. If you know in advance that you will run on multi core machine( spin lock on single core machine is not a good idea) and you know that you are going to spin for a relative short time( moving the thread to block state is a CPU intensive) then it might be OK

Shai Asher
- 80
- 6