我正在尝试使用 SpinLock,但是当我调用 SpinLock.Exit() 时,即使是单线程控制台应用程序中的这个最基本的代码也会引发以下异常
System.Threading.SynchronizationLockException was unhandled by user code
Message=The calling thread does not hold the lock. Source=mscorlib
这是整个源代码...
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");
}
}
}