SpinLock SynchronizationLockException - The calling thread does not hold the lock
Here is the code from a console app. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; namespace ConsoleApplication48 { class Program { static readonly SpinLock SpinLock = new SpinLock(); static void Main( string [] args) { bool lockTaken = false ; try { SpinLock.Enter( ref lockTaken); if (lockTaken) Console.WriteLine( " Lock taken " ); } finally { if (lockTaken) SpinLock.Exit(); } Console.WriteLine( " Done " ); } } } So why would this single-threaded app tell me that the thread trying to call SpinLock.Exit() doesn’t hold the lock? SpinLock is a value type. When you mark a field referencing a reference type as readonly you are not only making the field unass...