Table of Contents

Struct SpinWait

Namespace
System.Threading
Assembly
mscorlib.dll

Provides support for spin-based waiting.

public struct SpinWait
Inherited Members

Remarks

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 SpinWait(int) method.

While SpinWait is designed to be used in concurrent applications, it is not designed to be used from multiple threads concurrently. SpinWait's members are not thread-safe. If multiple threads must spin, each should use its own instance of SpinWait.

Methods

SpinOnce()

Performs a single spin.

public void SpinOnce()

Remarks

This is typically called in a loop, and may change in behavior based on the number of times a SpinOnce() has been called thus far on this instance.

SpinUntil(int)

Spins until the specified timeout is expired.

public static void SpinUntil(int millisecondsTimeout)

Parameters

millisecondsTimeout int

The number of milliseconds to wait.

Exceptions

ArgumentOutOfRangeException

millisecondsTimeout is a negative number other than -1, which represents an infinite time-out.

SpinUntil(TimeSpan)

Spins until the specified timeout is expired.

public static void SpinUntil(TimeSpan timeout)

Parameters

timeout TimeSpan

A TimeSpan that represents the number of milliseconds to wait.

Exceptions

ArgumentOutOfRangeException

timeout is a negative number other than -1 milliseconds, which represents an infinite time-out -or- timeout is greater than MaxValue.