Introduction
The .NET framework's ReaderWriterLock is slow. So slow that the overhead removes almost all benefits derived from it being a reader-writer lock (well, that is somewhat of an exaggeration). With the release of .NET framework 3.5, Microsoft introduced ReaderWriterLockSlim in an attempt to solve this problem. It is around 3 times faster than ReaderWriterLock (with no contention). Yet Microsoft did not design ReaderWriterLockSlim properly – it relies on an internal SpinLock. We can assume that the main reason you will be using a reader-writer lock is because there will be many concurrent readers and few writers, so a design which relies on a SpinLock even for concurrent shared (reader) acquisition will result in poor performance.
Spinlock
System.Threading.SpinLock 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.
When there is contention, SpinLock does not block, but spins in user mode. This avoids blocking, which requires a kernel-mode lock. Kernel Locks always cause an expensive context switch. After the spin, ideally the contention has been removed and execution can continue without the expensive context switch. SpinLock is ideal in scenarios involving frequent contention with short waits.
System.Threading.SpinLock is a low-level mutual exclusion lock that you can use for scenarios that have very short wait times. SpinLock is not re-entrant. After a thread enters the lock, it must exit the lock correctly before it can enter again. Typically, any attempt to re-enter the lock would cause deadlock, and deadlocks can be very difficult to debug. As an aid to development, System.Threading.SpinLock supports a thread-tracking mode that causes an exception to be thrown when a thread attempts to re-enter a lock that it already holds. This lets you more easily locate the point at which the lock was not exited correctly. You can turn on thread-tracking mode by using the SpinLock constructor that takes a Boolean input parameter, and ing in an argument of true. After you complete the development and testing phases, turn off thread-tracking mode for better performance.
The SpinLock type exposes the following members.
Constructors
SpinLock()- The constructor used to Initializes a new instance of the SpinLock structure.
Properties
- Isheld- The property gets whether the lock is currently held by any thread.
- IsheldByCurrentThread- The property gets whether the lock is held by the current thread.
- IsThreadOwnerTrackingEnabled- The property gets whether thread ownership tracking is enabled for this instance.
Method
- Enter(ref bool lockTaken)- To acquire the lock.
- Exit()- Release the lock.
- Finalize()- This method allows an object to try to release resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
The following code shows how to use the Enter() and Exit() methods to acquire and release locks:
using
System;
using
System.Threading;
namespace
SpinlockInthreading
{
public class
akshay
{
SpinLock spLock =
new SpinLock();
public void
process()
{
bool lockTaken =
false;
try
{
spLock.Enter(ref lockTaken);
Thread.Sleep(1000);
Console.WriteLine("process
method acquire the lock::");
}
finally
{
if (lockTaken) spLock.Exit();
Console.WriteLine("process
method Release the lock::");
}
}
public static
void Main()
{
akshay ak =
new akshay();
Thread th =
new Thread(new
ThreadStart(ak.process));
th.Start();
Console.Read();
}
}
}
output

However one difference with Monitor is, this lock is non reentrant in nature. In the following code the thread is trying to acquire the same SpinLock twice.



Aaron CronjePosted Jan 12, 2012, 11:04 PM
Good Work........