为什么我尝试在 C# 中实现基本的自旋锁会得到这个结果?

Why am I getting this result with my attempt to implement a basic spin-lock in C#?

尝试:

public interface ISemaphore
{
    void Aquire();

    void Release();
}

public class SpinlockSemaphore : ISemaphore
{
    private int _cur;
    private const int B = 1;
    private const int A = 0;

    public SpinlockSemaphore()
    {
        this._cur = A;
    }

    public void Aquire()
    {
        // Locally named variables for clarity. Could obviously be
        // condensed to something like:
        // while (Interlocked.CompareExchange(ref this._cur, B, A) != A);

        var flipped = false;
        do
        {
            // Flip to B only if it is A
            var prev = Interlocked.CompareExchange(ref this._cur, B, A);
            if (prev == A)
            {
                // If here, flipped from A to B
                flipped = true;
            }
            // If here, didn't flip because it was already B
        } while (!flipped);
    }

    public void Release()
    {
        // Set to A
        this._cur = A;
    }
}

测试(驱动程序):

var r = new Random();
ISemaphore s = new SpinlockSemaphore();
var t_base = DateTime.Now;

var tasks = Enumerable.Range(0, 26)
    .Select(i => Task.Run(() =>
    {
        var task = (char)((int)'A' + i);
        s.Aquire();
        Console.WriteLine($"I acquired the semaphore! (Task {task} at {(DateTime.Now - t_base).TotalMilliseconds} ms)");
        Thread.Sleep(r.Next() % 1000); // Simulate work
        s.Release();
        Console.WriteLine($"I released the semaphore! (Task {task} at {(DateTime.Now - t_base).TotalMilliseconds} ms)");
    }))
    .ToArray();

Task.WaitAll(tasks); // Run all tasks (concurrently)

Console.ReadKey(); // Keep Console window from closing

输出:发布前的收购示例

I acquired the semaphore! (Task B at 88.7629 ms)
I released the semaphore! (Task B at 635.2041 ms)
I acquired the semaphore! (Task C at 635.2041 ms)
I released the semaphore! (Task C at 906.7672 ms)
I acquired the semaphore! (Task D at 906.7672 ms)
I released the semaphore! (Task D at 1427.5738 ms)
I acquired the semaphore! (Task E at 1427.5738 ms)
I released the semaphore! (Task E at 1565.426 ms)
I acquired the semaphore! (Task A at 1565.426 ms)
I released the semaphore! (Task A at 2387.7001 ms)
I acquired the semaphore! (Task H at 2387.7001 ms)
I released the semaphore! (Task H at 3265.8058 ms)
I acquired the semaphore! (Task I at 3265.8058 ms)
I released the semaphore! (Task I at 3586.6978 ms)
I acquired the semaphore! (Task J at 3586.6978 ms)
I released the semaphore! (Task J at 3729.4946 ms)
I acquired the semaphore! (Task G at 3729.4946 ms)
I released the semaphore! (Task G at 4720.5139 ms)
I acquired the semaphore! (Task F at 4720.5139 ms)
I released the semaphore! (Task F at 5077.1144 ms)
I acquired the semaphore! (Task K at 5077.1144 ms)
I acquired the semaphore! (Task L at 5782.0138 ms)
I released the semaphore! (Task K at 5782.0138 ms)
I released the semaphore! (Task L at 6746.3076 ms)
I acquired the semaphore! (Task N at 6746.3076 ms)
I released the semaphore! (Task N at 7412.1375 ms)
I acquired the semaphore! (Task O at 7412.1375 ms)
I released the semaphore! (Task O at 8137.582 ms)
I acquired the semaphore! (Task M at 8137.582 ms)
I released the semaphore! (Task M at 8327.2192 ms)
I acquired the semaphore! (Task R at 8327.2192 ms)
I released the semaphore! (Task R at 8907.7793 ms)
I acquired the semaphore! (Task S at 8907.7793 ms)
I released the semaphore! (Task S at 9445.4624 ms)
I acquired the semaphore! (Task P at 9445.4624 ms)
I released the semaphore! (Task P at 10231.1811 ms)
I acquired the semaphore! (Task Q at 10231.1811 ms)
I released the semaphore! (Task Q at 10368.8125 ms)
I acquired the semaphore! (Task V at 10368.8125 ms)
I released the semaphore! (Task V at 10992.4072 ms)
I acquired the semaphore! (Task U at 10992.4072 ms)
I released the semaphore! (Task U at 12019.8456 ms)
I acquired the semaphore! (Task T at 12019.8456 ms)
I released the semaphore! (Task T at 12303.2822 ms)
I acquired the semaphore! (Task Y at 12303.2822 ms)
I released the semaphore! (Task Y at 12378.0044 ms)
I acquired the semaphore! (Task X at 12378.0044 ms)
I acquired the semaphore! (Task W at 12705.6469 ms)
I released the semaphore! (Task X at 12705.6469 ms)
I acquired the semaphore! (Task Z at 13671.7837 ms)
I released the semaphore! (Task W at 13671.7837 ms)
I released the semaphore! (Task Z at 14262.6353 ms)

到目前为止,我一直无法通过调试和重新思考交织来说服自己它应该起作用来找出问题。

我尝试通过暴力破解的方法:

我很想知道我忽略了什么明显的缺陷:)

即使你的自旋锁工作得很好(乍一看它是正确的,但使用这种代码很容易出错),你的驱动程序代码中仍然存在明显的竞争条件:

[...]
s.Release();
Console.WriteLine($"I released the semaphore! (Task {task} at {(DateTime.Now - t_base).TotalMilliseconds} ms)");

Console.WriteLine 没有被锁定,因此可能会发生下一个任务获取锁并打印获取消息 之前 释放任务打印其发布消息。

相反,您可以在 s.Release() 之前放置一条“我正在释放锁...”消息。如果您仍然看到相互交织的消息,则说明您的自旋锁已损坏。如果没有,它可能工作正常。