Click or drag to resize
SpinLock Class
A class that implements a simple lock that avoids the overhead imposed by using lock().
Inheritance Hierarchy
SystemObject
  VirtualRadar.InterfaceSpinLock

Namespace: VirtualRadar.Interface
Assembly: VirtualRadar.Interface (in VirtualRadar.Interface.dll) Version: 1.0.0.0 (1.0.0.0)
Syntax
public class SpinLock

The SpinLock type exposes the following members.

Constructors
  NameDescription
Public methodSpinLock
Initializes a new instance of the SpinLock class
Top
Methods
  NameDescription
Public methodAcquireLock
Locks the SpinLock and returns an object that unlocks the SpinLock when it is disposed.
Public methodEquals
Determines whether the specified Object is equal to the current Object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as a hash function for a particular type.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLock
Acquires the lock on the SpinLock, blocking indefinitely until it manages to acquire the lock. Ensure that Unlock is always called after Lock has returned.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodUnlock
Releases the lock on the SpinLock. This will unlock the SpinLock even if Lock was never called - only call this after Lock has returned.
Top
Remarks

You can create one of these objects to share amongst all of your threads and then call Lock to acquire a lock. If the object is already locked then the thread will spin until the lock can be acquired. Every call to Lockmust be paired with a call to Unlock, otherwise the lock remains held forever and every other thread will block.

A child class, SpinLockAcquire, exists to wrap the Lock and Unlock within a using statement, ensuring that the lock and unlock operations are always paired but at the expense of creating an object for it.

Unlike the traditional C# lock call a thread can lock itself if it calls Lock twice - care must be taken to avoid double-locks. What you gain in speed you lose in convenience.

See Also