gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:48:49 25 4
gpt4 key购买 nike

尝试:

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 更改为具有修饰符 volatile
  • Thread.MemoryBarrier() 乱扔代码

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

最佳答案

即使你的自旋锁工作完美(乍一看似乎是正确的,但这种代码很容易犯错误),你的驱动程序代码中也存在明显的竞争条件:

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

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

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

关于c# - 为什么我尝试在 C# 中实现基本自旋锁时会得到这个结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65205859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com