简体   繁体   English

使用 ManualResetEventSlim 而不是 ManualResetEvent 的最短等待时间是多少?

[英]What is the minimum wait time to use ManualResetEventSlim instead of ManualResetEvent?

From.NET 4 I can use the ManualResetEventSlim class that make a little spinning before blocking in order to get a time optimization if the blocking time is little (I have no context switch).从 .NET 4 我可以使用ManualResetEventSlim class 在阻塞之前稍微旋转一下,以便在阻塞时间很短的情况下获得时间优化(我没有上下文切换)。

I'd like to measure using a benchmark how little is this time in order to know, more or less, the amount of wait time necessary to prefer using a ManualResetEventSlim instead of a classic ManualResetEvent .我想使用基准来衡量这次的时间有多短,以便或多或少地知道更喜欢使用ManualResetEventSlim而不是经典的ManualResetEvent所需的等待时间。

I know that this measure is CPU dependent, it is impossible to know a priori the Spin time, but I'd like to have an order of magnitude.我知道这个度量是依赖于 CPU 的,不可能先验地知道自旋时间,但我想要一个数量级。

I wrote a benchmark class in order to get the minimum MillisecondSleep that make ManualResetEventSlim better than ManualResetEvent .我写了一个基准 class 以获得使ManualResetEventSlim优于ManualResetEvent的最小 MillisecondSleep。

public class ManualResetEventTest
{
    [Params(0, 1, 10)]
    public int MillisecondsSleep;

    [Benchmark]
    public void ManualResetEventSlim()
    {
        using var mres = new ManualResetEventSlim(false);
        var t = Task.Run(() =>
        {
            mres.Wait();
        });

        Thread.Sleep(MillisecondsSleep);
        mres.Set();
        t.Wait();
    }
    
    [Benchmark]
    public void ManualResetEvent()
    {
        using var mres = new ManualResetEvent(false);
        var t = Task.Run(() =>
        {
            mres.WaitOne();
        });

        Thread.Sleep(MillisecondsSleep);
        mres.Set();
        t.Wait();
    }
}

And the result is the following结果如下

基准测试结果

As you can see I found a improved performance only using a Thread.Sleep(0).如您所见,我发现仅使用 Thread.Sleep(0) 即可提高性能。 Furthermore I see a 15ms mean time with both 1 and 10 ms.此外,我看到 1 毫秒和 10 毫秒的平均时间均为 15 毫秒。 Am I missing something?我错过了什么吗?

Is it true that only with the 0 ms wait it is better to use a ManualResetEventSlim instead of ManualResetEvent ?是否只有 0 毫秒等待才更好地使用ManualResetEventSlim而不是ManualResetEvent

From the excellent C# 9.0 in a Nutshell book:从优秀的 C# 9.0 in a Nutshell 一书中:

Waiting or signaling an AutoResetEvent or ManualResetEvent takes about one microsecond (assuming no blocking).等待或发出AutoResetEventManualResetEvent信号大约需要一微秒(假设没有阻塞)。

ManualResetEventSlim and CountdownEvent can be up to 50 times faster in short-wait scenarios because of their nonreliance on the OS and judicious use of spinning constructs. ManualResetEventSlimCountdownEvent在短等待场景中最多可以快 50 倍,因为它们不依赖于操作系统并且明智地使用了旋转结构。 In most scenarios, however, the overhead of the signaling classes themselves doesn't create a bottleneck;然而,在大多数情况下,信令类本身的开销不会造成瓶颈; thus, it is rarely a consideration.因此,很少考虑。

Hopefully that's enough to give you a rough order of magnitude.希望这足以给你一个粗略的数量级。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM