SpinWait 구조체

정의

회전 기반 대기를 지원합니다.

public value class SpinWait
public struct SpinWait
type SpinWait = struct
Public Structure SpinWait
상속
SpinWait

예제

다음 예제에서는 사용 하는 방법을 보여 줍니다는 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 일반적인 회전 논리를 캡슐화합니다. 단일 프로세서 컴퓨터에서 생성 되는 트랜잭션 사용 중 대기 하는 대신 항상 사용 됩니다 및 Hyper-threading technology를 사용 하는 Intel 프로세서를 사용 하 여 컴퓨터 하드웨어 스레드 부족 문제를 막을 수 있습니다. SpinWait 회전 및 true 생성 좋은 혼합을 캡슐화합니다.

SpinWait 즉, 하위 수준의 코드 SpinWait 불필요 한 할당 오버 헤드의 걱정 없이 활용할 수는 값 형식이입니다. SpinWait 일반 애플리케이션에 일반적으로 유용 하지 않습니다. 대부분의 경우에서와 같은.NET Framework에서 제공 되는 동기화 클래스 사용 해야 Monitor합니다. 하지만 스핀 대기 인 필요 대부분의 용도 SpinWait 형식 보다 선호 해야는 Thread.SpinWait 메서드.

속성

Count

이 인스턴스에서 SpinOnce()가 호출된 횟수를 가져옵니다.

NextSpinWillYield

다음 SpinOnce() 호출이 프로세서를 생성하여 강제 컨텍스트 전환을 트리거할지 여부를 가져옵니다.

메서드

Reset()

회전 수를 다시 설정합니다.

SpinOnce()

단일 회전을 수행합니다.

SpinOnce(Int32)

단일 스핀을 수행하고 최소 스핀 수 이후에 Sleep(Int32)를 호출합니다.

SpinUntil(Func<Boolean>)

지정된 조건이 충족될 때까지 회전합니다.

SpinUntil(Func<Boolean>, Int32)

지정된 조건이 충족되거나 지정된 제한 시간이 만료될 때까지 회전합니다.

SpinUntil(Func<Boolean>, TimeSpan)

지정된 조건이 충족되거나 지정된 제한 시간이 만료될 때까지 회전합니다.

적용 대상

스레드 보안

하지만 SpinWait 는 동시 애플리케이션에서 사용할 수 있도록 설계를 설계 되지는 않았습니다 여러 스레드에서 동시에 사용할 수 있습니다. SpinWait 멤버는 스레드로부터 안전 하지 않습니다. 여러 스레드가 실행 해야 하는 경우에 고유한 인스턴스를 사용 하 여 다음 작업을 해야 각 SpinWait합니다.

추가 정보