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

Posted

技术标签:

【中文标题】为啥我尝试在 C# 中实现基本的自旋锁会得到这个结果?【英文标题】:Why am I getting this result with my attempt to implement a basic spin-lock in C#?为什么我尝试在 C# 中实现基本的自旋锁会得到这个结果? 【发布时间】:2021-03-20 04:24:49 【问题描述】:

尝试:

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)

到目前为止,我无法通过调试和重新考虑交错来说服自己它应该可以解决问题。

我尝试通过蛮力解决它的方法:

_cur 更改为具有修饰符volatileThread.MemoryBarrier()s 乱扔代码

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

【问题讨论】:

您是否假设各个线程写入Console 的行将按照与它们所代表的事件完全相同的顺序出现在Console 输出中?这可能不是一个有效的假设。例如,有一个异常情况“K L K L”,其中“获取”和“释放”似乎是无序的,但它们的数字时间戳完全相同。我认为您无法从输出中判断它们是否真的乱序的。 【参考方案1】:

即使您的自旋锁工作正常(乍一看似乎是正确的,但这种代码极易出错),您的驱动程序代码中仍存在明显的竞争条件:

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

Console.WriteLine 未锁定,因此可能发生下一个任务获取锁并打印获取消息在释放任务打印其释放消息之前

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

【讨论】:

以上是关于为啥我尝试在 C# 中实现基本的自旋锁会得到这个结果?的主要内容,如果未能解决你的问题,请参考以下文章

为啥我得到错误count():参数必须是在laravel中实现Countable的数组或对象?

隐藏在 c# 中的方法和一个有效的例子。为啥要在框架中实现?现实世界的优势是啥?

如何在 C# 中实现 glob

浅谈自旋锁和 JVM 对锁的优化

偏向锁+自旋锁+轻量级锁??????

如何在 C# 中实现线程关联?