public void Enter(ref bool taken)
{
if (Interlocked.CompareExchange(ref lockObj, 1, 0) != 0)
{
int count = 0;
while (Interlocked.CompareExchange(ref lockObj, 1, 0) != 0)
{
count++;
if (Environment.ProcessorCount > 1 && count <= 5)
Thread.SpinWait(25);
else
Thread.Sleep(0);
}
}
taken = true;
}