1 out of 1 rated this helpful - Rate this topic

SpinLock Structure

Provides a mutual exclusion lock primitive where a thread trying to acquire the lock waits in a loop repeatedly checking until the lock becomes available.

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)
[<Sealed>]
[<ComVisibleAttribute(false)>]
[<HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
    ExternalThreading = true)>]
type SpinLock =  struct end

The SpinLock type exposes the following members.

  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsSpinLockInitializes a new instance of the SpinLock structure with the option to track thread IDs to improve debugging.
Top
  NameDescription
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsHeldGets whether the lock is currently held by any thread.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsHeldByCurrentThreadGets whether the lock is held by the current thread.
Public propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsIsThreadOwnerTrackingEnabledGets whether thread ownership tracking is enabled for this instance.
Top
  NameDescription
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsEnterAcquires the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.
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 appsExit()Releases the lock.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsExit(Boolean)Releases the lock.
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 appsToStringReturns the fully qualified type name of this instance. (Inherited from ValueType.)
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTryEnter(Boolean)Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTryEnter(Int32, Boolean)Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.
Public methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsTryEnter(TimeSpan, Boolean)Attempts to acquire the lock in a reliable manner, such that even if an exception occurs within the method call, lockTaken can be examined reliably to determine whether the lock was acquired.
Top

For an example of how to use a Spin Lock, see How to: Use SpinLock for Low-Level Synchronization.

Spin locks can be used for leaf-level locks where the object allocation implied by using a Monitor, in size or due to garbage collection pressure, is overly expensive. A spin lock can be useful in to avoid blocking; however, if you expect a significant amount of blocking, you should probably not use spin locks due to excessive spinning. Spinning can be beneficial when locks are fine-grained and large in number (for example, a lock per node in a linked list) and also when lock hold-times are always extremely short. In general, while holding a spin lock, one should avoid any of these actions:

  • blocking,

  • calling anything that itself may block,

  • holding more than one spin lock at once,

  • making dynamically dispatched calls (interface and virtuals),

  • making statically dispatched calls into any code one doesn't own, or

  • allocating memory.

SpinLock should only be used after you have been determined that doing so will improve an application's performance. It is also important to note that SpinLock is a value type, for performance reasons. For this reason, you must be very careful not to accidentally copy a SpinLock instance, as the two instances (the original and the copy) would then be completely independent of one another, which would likely lead to erroneous behavior of the application. If a SpinLock instance must be passed around, it should be passed by reference rather than by value.

Do not store SpinLock instances in readonly fields.

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 SpinLock:

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

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

All members of SpinLock are thread-safe and may be used from multiple threads concurrently.

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