READER/WRITER LOCK in Visual C#

Print Code 128 Code Set C in Visual C# READER/WRITER LOCK

READER/WRITER LOCK
Code 128B Generation In C#.NET
Using Barcode drawer for VS .NET Control to generate, create Code 128B image in .NET framework applications.
www.OnBarcode.com
Scanning ANSI/AIM Code 128 In C#
Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
requests a write lock, it is given the same priority as a thread that is converting from a reader lock. If the reader request queue was a priority queue, the threads that had obtained a read lock could potentially starve the threads that attempted a write lock request directly. Listing 9.8 demonstrates downgrading from a writer to a reader lock. Note that this can only be performed if the thread originally obtained a read lock and used the UpgradeToWriterLock method. The cookie returned by UpgradeToWriterLock can only be used with DowngradeFromWriterLock.
EAN / UCC - 14 Creator In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create UCC-128 image in .NET framework applications.
www.OnBarcode.com
Painting Matrix Barcode In Visual C#
Using Barcode maker for VS .NET Control to generate, create 2D Barcode image in .NET applications.
www.OnBarcode.com
Listing 9.8 Using the DowngradeFromWriterLock method
Code 128 Code Set B Encoder In C#.NET
Using Barcode creation for VS .NET Control to generate, create USS Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing Code-39 In Visual C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code 3/9 image in .NET framework applications.
www.OnBarcode.com
using System; using System.Threading; namespace Manning.Dennis { public class DataUD:ThreadedTesterBase { ManualResetEvent[] interactEvents; public DataUD(ref Data pd,string n,string v) :base(ref pd,n,v) { interactEvents =new ManualResetEvent[4]; for (int i=0;i< interactEvents .Length;i++) { interactEvents[i]=new ManualResetEvent(false); } } public void Interact(ActionsEnum index) { interactEvents[(int)index].Set(); // Give the associated thread time to do its thing Thread.Sleep(1000); } public enum ActionsEnum { UpgradeToWrite=0, DowngradeToRead=1, ReleaseRead=2 } protected override void ThreadMethod() { LockCookie cookie; Message("Enter"); acquireEvent.WaitOne(); Message("Starting Wait for Read Lock"); protectedData.rwLock.AcquireReaderLock(Timeout.Infinite); Message("+++ UD- Acquired Read Lock"); string s = protectedData.Value;
Barcode Creator In Visual C#.NET
Using Barcode maker for Visual Studio .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
Draw RoyalMail4SCC In C#.NET
Using Barcode encoder for Visual Studio .NET Control to generate, create British Royal Mail 4-State Customer Code image in .NET applications.
www.OnBarcode.com
ACQUIRING A WRITER LOCK FROM A READERWRITERLOCK
Code 128C Encoder In None
Using Barcode creation for Font Control to generate, create ANSI/AIM Code 128 image in Font applications.
www.OnBarcode.com
Creating Code 128 Code Set B In Visual Studio .NET
Using Barcode encoder for .NET framework Control to generate, create Code 128 Code Set A image in .NET applications.
www.OnBarcode.com
interactEvents[(int)ActionsEnum.UpgradeToWrite].WaitOne(); Message("^^^ UD- Upgrading Read Lock"); cookie=protectedData.rwLock.UpgradeToWriterLock(Timeout.Infinite); protectedData.Value= valueToWrite;
Scan PDF-417 2d Barcode In C#
Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
QR Maker In .NET
Using Barcode creation for Reporting Service Control to generate, create QR Code 2d barcode image in Reporting Service applications.
www.OnBarcode.com
Convert the read lock to a write lock
Encoding Code-39 In Java
Using Barcode generation for Java Control to generate, create Code 3 of 9 image in Java applications.
www.OnBarcode.com
USS-128 Encoder In Java
Using Barcode encoder for Android Control to generate, create GS1 128 image in Android applications.
www.OnBarcode.com
interactEvents[(int)ActionsEnum.DowngradeToRead].WaitOne(); Message("vvv UD- Downgrading Read Lock"); protectedData.rwLock.DowngradeFromWriterLock(ref cookie); string s2 = protectedData.Value; Change back interactEvents[(int)ActionsEnum.ReleaseRead].WaitOne(); Message(" UD- Releasing Read Lock"); protectedData.rwLock.ReleaseReaderLock(); Message("---Released Read Lock"); } } }
Paint DataMatrix In None
Using Barcode maker for Online Control to generate, create Data Matrix image in Online applications.
www.OnBarcode.com
Painting GS1-128 In None
Using Barcode generator for Word Control to generate, create GTIN - 128 image in Microsoft Word applications.
www.OnBarcode.com
to a read lock
QR Code Reader In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Make Barcode In .NET Framework
Using Barcode creation for .NET framework Control to generate, create Barcode image in Visual Studio .NET applications.
www.OnBarcode.com
One of the biggest advantages of the DowngradeFromWriterLock method is that it will not block. This means that it will immediately return granting the thread a read lock because there cannot possibly be a read lock at the point a write lock has been granted. Additionally, at the point the write lock is released, all pending read locks will also be released. Listing 9.8 uses a base class that reduces the complexity of the DataUD class. Other classes use this base class. Listing 9.9 contains the base class code.
Code 128 Code Set C Generation In None
Using Barcode generator for Software Control to generate, create Code 128A image in Software applications.
www.OnBarcode.com
Data Matrix Maker In None
Using Barcode creator for Software Control to generate, create Data Matrix image in Software applications.
www.OnBarcode.com
Listing 9.9 The base class that listing 9.8 relies on
using System; using System.Threading; namespace Manning.Dennis { public abstract class ThreadedTesterBase { protected string valueToWrite; protected bool acquireCalled; protected bool interactCalled; protected ManualResetEvent acquireEvent; protected ManualResetEvent interactEvent; protected Data protectedData; protected Thread workerThread; protected string name; protected void Message(string msg) { protectedData.Message(msg); } public void Acquire() { acquireCalled = true; acquireEvent.Set();
READER/WRITER LOCK
// Give the associated thread time to do its thing Thread.Sleep(1000); } public void Interact() { if (!acquireCalled) { throw new Exception("Call Acquire first"); } interactCalled = true; interactEvent.Set(); // Give the associated thread time to do its thing Thread.Sleep(1000); } protected ThreadedTesterBase(ref Data pd,string name,string valueToWrite) { this.valueToWrite = valueToWrite; acquireCalled = false; interactCalled = false; acquireEvent = new ManualResetEvent(false); interactEvent = new ManualResetEvent(false); this.protectedData = pd; workerThread = new Thread(new ThreadStart(ThreadMethod)); workerThread.Name = name; this.name = name; workerThread.Start(); } protected abstract void ThreadMethod(); public void WaitForFinish() { workerThread.Join(); // Give the associated thread time to do its thing Thread.Sleep(0); } } }
This base class simplifies the creation of threads used during the testing process. Listing 9.10 contains code that drives the example.
Listing 9.10 Code that demonstrates that a DowngradeFromWriterLock does not block
public void UpgradeDowngradeExample() { Data pdata = new Data(); DataWriter w1; DataWriter w2; DataUD ud1;
ACQUIRING A WRITER LOCK FROM A READERWRITERLOCK
ud1= new DataUD (ref pdata,"Upgrader1: {0}","Upgrader1"); w1= new DataWriter(ref pdata,"Writer_1 : {0}","writer_1"); w2= new DataWriter(ref pdata,"Writer_2 : {0}","writer_2"); Thread.Sleep(1000); w1.Acquire(); // acquire write lock ud1.Acquire(); ud1.Interact(DataUD.ActionsEnum.UpgradeToWrite); w1.Interact(); // set value and release lock w2.Acquire(); // acquire write lock w2.Interact(); // set value and release lock ud1.Interact(DataUD.ActionsEnum.DowngradeToRead); ud1.Interact(DataUD.ActionsEnum.ReleaseRead); w1.WaitForFinish(); Console.WriteLine("Enter to exit"); Console.ReadLine(); }
The DataWriter class is contained in listing 9.11.
Listing 9.11 DataWriter class
using System; using System.Threading; namespace Manning.Dennis { public class DataWriter :ThreadedTesterBase { public DataWriter(ref Data protectedData,string name,string valueToWrite) :base(ref protectedData,name,valueToWrite) { } protected override void ThreadMethod() { Message("Enter"); acquireEvent.WaitOne(); Message("Starting Wait for Write Lock"); protectedData.rwLock.AcquireWriterLock(Timeout.Infinite); Message("+++Acquired Writer Lock"); interactEvent.WaitOne(); Message("Setting value"); protectedData.Value=valueToWrite; Message(" Releasing Writer Lock"); protectedData.rwLock.ReleaseWriterLock(); Message("---Released Writer Lock"); } } }
READER/WRITER LOCK
A caution regarding upgrading and downgrading reader locks: A lock should be shortlived. This will increase concurrency and decrease contention for locks. If a thread goes from being a reader to a writer and back to a reader, and stays in that state for an extended period of time, other threads will not be able to acquire a write lock. In general, locks should not be held the vast majority of the time, and only acquired when needed. The general rule of acquiring late and releasing early applies. 9.2.4 WriterSeqNum and AnyWritersSince Suppose you wanted to know if any changes had occurred since you acquired and released a reader lock. One way to determine this is to use the WriterSeqNum property of the ReaderWriterLock object. This property returns a value that can be used with the AnyWritersSince method to determine if any writer locks have been released since WriterSeqNum was acquired.
Listing 9.12 WriterSeqNum can be used to see if data has changed (VB.NET).
Public Sub Bid(ByVal Amount As Decimal, ByVal BiddersName As String) Dim WriterSeqNum As Integer . . . ItemLock.AcquireReaderLock(Timeout.Infinite) Retrieve the writer . . . sequence number If (Amount > TheCurrentPrice) Then and save it WriterSeqNum = ItemLock.WriterSeqNum ItemLock.ReleaseReaderLock() Thread.Sleep(1000) ' Make the changes more obvious ItemLock.AcquireWriterLock(Timeout.Infinite) If (ItemLock.AnyWritersSince(WriterSeqNum)) Then Look for If (Amount > TheCurrentPrice) Then new writers TheCurrentPrice = Amount TheBiddersName = BiddersName Else Throw New Exception("Bid not higher than current price ") End If Else TheCurrentPrice = Amount TheBiddersName = BiddersName End If Else Throw New Exception("Bid not higher than current price") End If
In listing 9.12 we first acquire a reader lock. To simplify the code we wait indefinitely for the lock. Once the reader lock is acquired we retrieve the writer sequence number the number of nonnested times a write lock has been acquired and released. It starts at 1 and increases by 1 each time ReleaseWriterLock is invoked by a thread that results in that thread no longer owning the write lock.
Copyright © OnBarcode.com . All rights reserved.