Here is the code from a console app.
static readonly SpinLock SpinLock = new SpinLock();
static void Main(string args)
bool lockTaken = false;
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 unassignable but also implicitly making the value immutable. To achieve this .NET will give you a copy of the value each time you read it rather than returning the original, that way if you change anything you will in fact be changing a copy. So the call to SpinLock.Enter will execute on one copy of the SpinLock, and SpinLock.Exit will execute on a second copy and thus because they are separate instances the call to Exit will throw an exception because the current thread doesn’t hold a lock on it.
The solution is simple; remove readonly.