Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

SpinWait Structure

Provides support for spin-based waiting.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
	ExternalThreading = true)]
public struct SpinWait

The SpinWait type exposes the following members.

  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsCountGets the number of times SpinOnce has been called on this instance.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsNextSpinWillYieldGets whether the next call to SpinOnce will yield the processor, triggering a forced context switch.
Top
  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsIndicates whether this instance and a specified object are equal. (Inherited from ValueType.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeReturns the hash code for this instance. (Inherited from ValueType.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsResetResets the spin counter.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpinOncePerforms a single spin.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpinUntil(Func<Boolean>)Spins until the specified condition is satisfied.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpinUntil(Func<Boolean>, Int32)Spins until the specified condition is satisfied or until the specified timeout is expired.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpinUntil(Func<Boolean>, TimeSpan)Spins until the specified condition is satisfied or until the specified timeout is expired.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns the fully qualified type name of this instance. (Inherited from ValueType.)
Top

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.

NoteNote

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:

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);
    }
}

.NET Framework

Supported in: 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.