SpinWait Struct
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Provides support for spin-based waiting.
public value class SpinWait
public struct SpinWait
type SpinWait = struct
Public Structure SpinWait
- Inheritance
The 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 encapsulates common spinning logic. 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 encapsulates a good mixture of spinning and true yielding.
SpinWait is a value type, which means that low-level code can utilize SpinWait without fear of unnecessary allocation overheads. SpinWait is not generally useful for ordinary applications. In most cases, you should use the synchronization classes provided by the .NET Framework, such as Monitor. For most purposes where spin waiting is required, however, the SpinWait type should be preferred over the Thread.SpinWait method.
Count |
Gets the number of times SpinOnce() has been called on this instance. |
Next |
Gets whether the next call to SpinOnce() will yield the processor, triggering a forced context switch. |
Reset() |
Resets the spin counter. |
Spin |
Performs a single spin. |
Spin |
Performs a single spin and calls Sleep(Int32) after a minimum spin count. |
Spin |
Spins until the specified condition is satisfied or until the specified timeout is expired. |
Spin |
Spins until the specified condition is satisfied or until the specified timeout is expired. |
Spin |
Spins until the specified condition is satisfied. |
Produkt | Verze |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
While SpinWait is designed to be used in concurrent applications, it is not designed to be used from multiple threads concurrently. SpinWait members are not thread-safe. If multiple threads must spin, each should use its own instance of SpinWait.
Zpětná vazba k produktu .NET
.NET je open source projekt. Vyberte odkaz pro poskytnutí zpětné vazby: