C#: Spinwait в поле int (действующее как bool) против SpinLock?
В чем эти два различия?
public class SpinWaitLocking
{
private int _isLock;
public void TestMethod1()
{
var sw = new SpinWait();
while (Interlocked.CompareExchange(ref _isLock, 1, 0) == 0)
{
sw.SpinOnce();
}
DoSomethingQuick1();
_isLock = 0;
}
public void TestMethod2()
{
var sw = new SpinWait();
while (Interlocked.CompareExchange(ref _isLock, 1, 0) == 0)
{
sw.SpinOnce();
}
DoSomethingQuick2();
_isLock = 0;
}
}
public class SpinLocking
{
private SpinLock _spinLock = new SpinLock();
private bool _isLock;
public void TestMethod1()
{
_spinLock.Enter(ref _isLock);
DoSomethingQuick1();
_spinLock.Exit();
}
public void TestMethod2()
{
_spinLock.Enter(ref _isLock);
DoSomethingQuick2();
_spinLock.Exit();
}
}