SpinLock Class |
Namespace: VirtualRadar.Interface
The SpinLock type exposes the following members.
Name | Description | |
---|---|---|
AcquireLock |
Locks the SpinLock and returns an object that unlocks the SpinLock when it is disposed.
| |
Equals | (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Lock |
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.
| |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Unlock |
Releases the lock on the SpinLock. This will unlock the SpinLock even if Lock was never called - only call this after Lock
has returned.
|
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.