This documentation is archived and is not being maintained.

SpinWait Structure

Provides support for spin-based waiting.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[HostProtectionAttribute(SecurityAction::LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public value class SpinWait

The SpinWait type exposes the following members.

Public propertyCountGets the number of times SpinOnce has been called on this instance.
Public propertyNextSpinWillYieldGets whether the next call to SpinOnce will yield the processor, triggering a forced context switch.

Public methodEqualsIndicates whether this instance and a specified object are equal. (Inherited from ValueType.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from ValueType.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodResetResets the spin counter.
Public methodSpinOncePerforms a single spin.
Public methodStatic memberSpinUntil(Func<Boolean>)Spins until the specified condition is satisfied.
Public methodStatic memberSpinUntil(Func<Boolean>, Int32)Spins until the specified condition is satisfied or until the specified timeout is expired.
Public methodStatic memberSpinUntil(Func<Boolean>, TimeSpan)Spins until the specified condition is satisfied or until the specified timeout is expired.
Public methodToStringReturns the fully qualified type name of this instance. (Inherited from ValueType.)

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 method.


The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: Synchronization | ExternalThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example shows how to use a SpinWait:

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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.