Using multithreading: SpinLock throwing SynchronizationLockException on newest questions tagged multithreading – Stack Overflow

I am trying to use SpinLock, but even this most basic code in a single threaded Console app throws the following exception when I callSpinLock.Exit()

System.Threading.SynchronizationLockException was unhandled by user code
  Message=The calling thread does not hold the lock.  Source=mscorlib

Here is the entire source code…

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");
        }
    }
}

See Answers


source: http://stackoverflow.com/questions/11224130/spinlock-throwing-synchronizationlockexception
Using multithreading: using-multithreading



online applications demo