Ich versuche SpinLock zu verwenden, aber auch diese grundlegendsten Code in einer Single-Threaded-Konsole app wirft die folgende Ausnahme, wenn ich callSpinLock.Exit()SpinLock werfen SynchronizationLockException
System.Threading.SynchronizationLockException was unhandled by user code
Message=The calling thread does not hold the lock. Source=mscorlib
Hier ist die gesamte Quellcode. ..
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");
}
}
}
Die Nachricht sagt, dass die Sperre nicht im Besitz desselben Threads ist, der Exit aufruft - aber aus dem Code ist es eindeutig. –