SemaphoreSlim (.NET) 是否阻止同一线程进入块?

Does SemaphoreSlim (.NET) prevent same thread from entering block?

我已经阅读了 SemaphoreSlim 的文档 SemaphoreSlim MSDN 这表明如果您将 SemaphoreSlim 配置为:

,则 SemaphoreSlim 将一次仅通过一个线程限制一段代码为 运行
SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);

但是,它并不表示它是否会阻止 相同 线程访问该代码。这与 async 和 await 一起出现。如果在方法中使用 await ,控制会在任何任务或线程完成时离开该方法和 returns 。在我的示例中,我使用带有异步按钮处理程序的按钮。它使用 'await' 调用另一个方法 (Function1)。 Function1 依次调用

await Task.Run(() => Function2(beginCounter));

我的 Task.Run() 周围有一个 SemaphoreSlim。它似乎确实阻止了同一个线程进入 Function2。但这并不能从文档中得到保证(正如我所读到的),我想知道是否可以指望这一点。

我已经在下面发布了我的完整示例。

谢谢,

戴夫

 using System;
 using System.Threading;
 using System.Threading.Tasks;
 using System.Windows;

 namespace AsynchAwaitExample
 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    private readonly SemaphoreSlim _semaphoreSlim = new SemaphoreSlim(1, 1);
    public MainWindow()
    {
        InitializeComponent();
    }

    static int beginCounter = 0;
    static int endCounter = 0;
    /// <summary>
    /// Suggest hitting button 3 times in rapid succession
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void button_Click(object sender, RoutedEventArgs e)
    {
        beginCounter++;
        endCounter++;
        // Notice that if you click fast, you'll get all the beginCounters first, then the endCounters
        Console.WriteLine("beginCounter: " + beginCounter + " threadId: " + Thread.CurrentThread.ManagedThreadId);
        await Function1(beginCounter);
        Console.WriteLine("endCounter: " + endCounter + " threadId: " + Thread.CurrentThread.ManagedThreadId);
    }

    private async Task Function1(int beginCounter)
    {
        try
        {
            Console.WriteLine("about to grab lock" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
            await _semaphoreSlim.WaitAsync();  // get rid of _semaphoreSlim calls and you'll get into beginning of Function2 3 times before exiting
            Console.WriteLine("grabbed lock" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
            await Task.Run(() => Function2(beginCounter));
        }
        finally
        {
            Console.WriteLine("about to release lock" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
            _semaphoreSlim.Release();
            Console.WriteLine("released lock" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
        }

    }

    private void Function2(int beginCounter)
    {
        Console.WriteLine("Function2 start" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
        Thread.Sleep(1000);
        Console.WriteLine("Function2 end" + " threadId: " + Thread.CurrentThread.ManagedThreadId + " beginCounter: " + beginCounter);
        return;
    }
}
}

如果单击按钮 3 次,输出示例。请注意,对于给定的计数器,Function2 总是在再次启动之前完成。

    beginCounter: 1 threadId: 9
about to grab lock threadId: 9 beginCounter: 1
grabbed lock threadId: 9 beginCounter: 1
Function2 start threadId: 13 beginCounter: 1
beginCounter: 2 threadId: 9
about to grab lock threadId: 9 beginCounter: 2
beginCounter: 3 threadId: 9
about to grab lock threadId: 9 beginCounter: 3
Function2 end threadId: 13 beginCounter: 1
about to release lock threadId: 9 beginCounter: 1
released lock threadId: 9 beginCounter: 1
grabbed lock threadId: 9 beginCounter: 2
Function2 start threadId: 13 beginCounter: 2
endCounter: 3 threadId: 9
Function2 end threadId: 13 beginCounter: 2
about to release lock threadId: 9 beginCounter: 2
released lock threadId: 9 beginCounter: 2
endCounter: 3 threadId: 9
grabbed lock threadId: 9 beginCounter: 3
Function2 start threadId: 13 beginCounter: 3
Function2 end threadId: 13 beginCounter: 3
about to release lock threadId: 9 beginCounter: 3
released lock threadId: 9 beginCounter: 3
endCounter: 3 threadId: 9

如果你摆脱了 SemaphoreSlim 调用,你将得到:

beginCounter: 1 threadId: 10
about to grab lock threadId: 10 beginCounter: 1
grabbed lock threadId: 10 beginCounter: 1
Function2 start threadId: 13 beginCounter: 1
beginCounter: 2 threadId: 10
about to grab lock threadId: 10 beginCounter: 2
grabbed lock threadId: 10 beginCounter: 2
Function2 start threadId: 14 beginCounter: 2
beginCounter: 3 threadId: 10
about to grab lock threadId: 10 beginCounter: 3
grabbed lock threadId: 10 beginCounter: 3
Function2 start threadId: 15 beginCounter: 3
Function2 end threadId: 13 beginCounter: 1
about to release lock threadId: 10 beginCounter: 1
released lock threadId: 10 beginCounter: 1
endCounter: 3 threadId: 10
Function2 end threadId: 14 beginCounter: 2
about to release lock threadId: 10 beginCounter: 2
released lock threadId: 10 beginCounter: 2
endCounter: 3 threadId: 10

来自the documentation

The SemaphoreSlim class doesn’t enforce thread or task identity on calls to the Wait, WaitAsync, and Release methods

换句话说,class 不查看哪个线程正在调用它。这只是一个简单的计数器。同一个线程可以多次获取信号量,这与多个线程获取信号量是一样的。如果剩余的线程数下降到 0,那么即使一个线程已经是那个线程获取信号量的线程,如果它调用 Wait(),它会阻塞直到其他线程释放信号量。

因此,关于 async/awaitawait 可能会或可能不会在启动它的同一线程中恢复这一事实并不重要。只要您保持 Wait()Release() 调用平衡,它就会像人们希望和期望的那样工作。

在您的示例中,您甚至异步等待信号量,因此不会阻塞任何线程。这很好,否则你会在第二次按下按钮时死锁 UI 线程。


相关阅读:
Resource locking between iterations of the main thread (Async/Await)
Why does this code not end in a deadlock
Locking with nested async calls

特别注意 re-entrant/recursive 锁定的注意事项,尤其是 async/await。线程同步已经够棘手了,而这个困难正是 async/await 旨在简化的。在大多数情况下,它的作用非常显着。但当你将它与另一种 synchronization/locking 机制混合使用时就不会了。