SpinLock Structure

.NET Framework (current version)
 

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

NameDescription
System_CAPS_pubmethodSpinLock(Boolean)

Initializes a new instance of the SpinLock structure with the option to track thread IDs to improve debugging.

NameDescription
System_CAPS_pubpropertyIsHeld

Gets whether the lock is currently held by any thread.

System_CAPS_pubpropertyIsHeldByCurrentThread

Gets whether the lock is held by the current thread.

System_CAPS_pubpropertyIsThreadOwnerTrackingEnabled

Gets whether thread ownership tracking is enabled for this instance.

NameDescription
System_CAPS_pubmethodEnter(Boolean)

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

System_CAPS_pubmethodEquals(Object)

Indicates whether this instance and a specified object are equal.(Inherited from ValueType.)

System_CAPS_pubmethodExit()

Releases the lock.

System_CAPS_pubmethodExit(Boolean)

Releases the lock.

System_CAPS_pubmethodGetHashCode()

Returns the hash code for this instance.(Inherited from ValueType.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodToString()

Returns the fully qualified type name of this instance.(Inherited from ValueType.)

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

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

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

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.

The following example shows how to use a SpinLock:

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

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1

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

Return to top
Show: