SpinWait 结构

定义

为基于自旋的等待提供支持。Provides support for spin-based waiting.

public value class SpinWait
public struct SpinWait
type SpinWait = struct
Public Structure SpinWait
继承
SpinWait

示例

下面的示例演示如何使用 SpinWaitThe following example shows how to use a SpinWait:

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

class SpinWaitDemo
{
    // Demonstrates:
    //      SpinWait construction
    //      SpinWait.SpinOnce()
    //      SpinWait.NextSpinWillYield
    //      SpinWait.Count
    static void Main()
    {
        bool someBoolean = false;
        int numYields = 0;

        // First task: SpinWait until someBoolean is set to true
        Task t1 = Task.Factory.StartNew(() =>
        {
            SpinWait sw = new SpinWait();
            while (!someBoolean)
            {
                // The NextSpinWillYield property returns true if
                // calling sw.SpinOnce() will result in yielding the
                // processor instead of simply spinning.
                if (sw.NextSpinWillYield) numYields++;
                sw.SpinOnce();
            }

            // As of .NET Framework 4: After some initial spinning, SpinWait.SpinOnce() will yield every time.
            Console.WriteLine("SpinWait called {0} times, yielded {1} times", sw.Count, numYields);
        });

        // Second task: Wait 100ms, then set someBoolean to true
        Task t2 = Task.Factory.StartNew(() =>
        {
            Thread.Sleep(100);
            someBoolean = true;
        });

        // Wait for tasks to complete
        Task.WaitAll(t1, t2);
    }
}
Imports System.Threading
Imports System.Threading.Tasks

Module SpinWaitDemo
    ' Demonstrates:
    ' SpinWait construction
    ' SpinWait.SpinOnce()
    ' SpinWait.NextSpinWillYield
    ' SpinWait.Count
    Private Sub SpinWaitSample()
        Dim someBoolean As Boolean = False
        Dim numYields As Integer = 0

        ' First task: SpinWait until someBoolean is set to true
        Dim t1 As Task = Task.Factory.StartNew(
            Sub()
                Dim sw As New SpinWait()
                While Not someBoolean
                    ' The NextSpinWillYield property returns true if
                    ' calling sw.SpinOnce() will result in yielding the
                    ' processor instead of simply spinning.
                    If sw.NextSpinWillYield Then
                        numYields += 1
                    End If
                    sw.SpinOnce()
                End While

                ' As of .NET Framework 4: After some initial spinning, SpinWait.SpinOnce() will yield every time.
                Console.WriteLine("SpinWait called {0} times, yielded {1} times", sw.Count, numYields)
            End Sub)

        ' Second task: Wait 100ms, then set someBoolean to true
        Dim t2 As Task = Task.Factory.StartNew(
            Sub()
                Thread.Sleep(100)
                someBoolean = True
            End Sub)

        ' Wait for tasks to complete
        Task.WaitAll(t1, t2)
    End Sub

End Module

注解

SpinWait 封装常见旋转逻辑。SpinWait encapsulates common spinning logic. 在单处理器计算机上,始终使用 "生成" 而不是 "繁忙等待",在装有超线程技术的 Intel 处理器的计算机上,这有助于防止硬件线程不足。On single-processor machines, yields are always used instead of busy waits, and on computers with Intel processors employing Hyper-Threading technology, it helps to prevent hardware thread starvation. SpinWait 封装了一种很好的旋转和真正的生成。SpinWait encapsulates a good mixture of spinning and true yielding.

SpinWait 是一种值类型,这意味着低级别代码可以使用 SpinWait,而不必担心不必要的分配开销。SpinWait is a value type, which means that low-level code can utilize SpinWait without fear of unnecessary allocation overheads. SpinWait 对于普通应用程序通常不起作用。SpinWait is not generally useful for ordinary applications. 在大多数情况下,应使用 .NET Framework 提供的同步类,如 MonitorIn most cases, you should use the synchronization classes provided by the .NET Framework, such as Monitor. 但是,在需要自旋等待的大多数情况下,SpinWait 类型应优先于 Thread.SpinWait 方法。For most purposes where spin waiting is required, however, the SpinWait type should be preferred over the Thread.SpinWait method.

属性

Count

获取已对此实例调用 SpinOnce() 的次数。Gets the number of times SpinOnce() has been called on this instance.

NextSpinWillYield

获取对 SpinOnce() 的下一次调用是否将产生处理器,同时触发强制上下文切换。Gets whether the next call to SpinOnce() will yield the processor, triggering a forced context switch.

方法

Reset()

重置自旋计数器。Resets the spin counter.

SpinOnce()

执行单一自旋。Performs a single spin.

SpinOnce(Int32)

执行单一自旋,并在达到最小旋转计数后调用 Sleep(Int32)Performs a single spin and calls Sleep(Int32) after a minimum spin count.

SpinUntil(Func<Boolean>)

在指定条件得到满足之前自旋。Spins until the specified condition is satisfied.

SpinUntil(Func<Boolean>, Int32)

在指定条件得到满足或指定超时过期之前自旋。Spins until the specified condition is satisfied or until the specified timeout is expired.

SpinUntil(Func<Boolean>, TimeSpan)

在指定条件得到满足或指定超时过期之前自旋。Spins until the specified condition is satisfied or until the specified timeout is expired.

适用于

线程安全性

虽然 SpinWait 旨在在并发应用程序中使用,但它并不是从多个线程同时使用而设计的。While SpinWait is designed to be used in concurrent applications, it is not designed to be used from multiple threads concurrently. SpinWait 成员不是线程安全的。SpinWait members are not thread-safe. 如果多个线程必须旋转,每个线程都应该使用其自己的 SpinWait实例。If multiple threads must spin, each should use its own instance of SpinWait.

另请参阅