Threading in Delphi for .NET - Locks that Distinguish Between Readers and Writers
(Page 11 of 15 )
The System.Threading.ReaderWriterLock Class
The Mutex and Monitor classes provide a locking mechanism that is useful for preventing a section of code from being executed simultaneously by multiple threads. However, there are times when a lock that can distinguish between a reader and a writer can increase performance. This is especially true when there are more readers than writers or if the writer is infrequent with its updates. Fortunately, the ReaderWriterLock class provides this functionality. Listing 14.10 contains the definition of the ReaderWriterLock class.
Listing 14.10 Declaration of the System.Threading.ReaderWriterLock Class
System.Threading.ReaderWriterLock = class (System.Object)
constructor Create;
procedure AcquireReaderLock(millisecondsTimeout: Integer); overload;
procedure AcquireReaderLock(timeout: TimeSpan); overload;
procedure AcquireWriterLock(millisecondsTimeout: Integer); overload;
procedure AcquireWriterLock(timeout: TimeSpan); overload;
procedure ReleaseReaderLock;
procedure ReleaseWriterLock;
function UpgradeToWriterLock(millisecondsTimeout: Integer) :
LockCookie; overload;
function UpgradeToWriterLock(timeout: TimeSpan) :
LockCookie; overload;
procedure DowngradeFromWriterLock(var lockCookie: LockCookie);
function ReleaseLock: LockCookie;
procedure RestoreLock(var lockCookie: LockCookie);
function AnyWritersSince(seqNum: Integer) : Boolean;
property IsReaderLockHeld: Boolean read;
property IsWriterLockHeld: Boolean read;
property WriterSeqNum: integer read;
end;
As expected, two types of locks are available. These locks are referred to as a reader lock and a writer lock. Locks are acquired by using the AcquireReaderLock() and AcquireWriterLock() methods, respectively.
A reader lock is acquired only when no writer locks are being held. That way, multiple readers are allowed. However, all readers are blocked when a writer lock is obtained. Only one writer lock is allowed. Find the code on the CD: \Code\Chapter 14\Ex07\TestRWLock.dpr for an example of how to use the ReaderWriterLock class.
The System.Threading.Interlocked Class
Because of the random nature of the way the kernel schedules threads, there is no way to prevent a block of code from being interrupted to execute another thread. Code that needs to be executed atomically—that is, without interruption—requires the use of the System.Threading.Interlocked class. Listing 14.11 contains the class definition of the Interlocked class.
Listing 14.11 Declaration of the System.Threading.Interlocked Class
System.Threading.Interlocked = class (System.Object)
class function Increment(var location: Integer) : Integer; overload; static;
class function Increment(var location: Int64) : Int64; overload; static;
class function Decrement(var location: Integer) : Integer; overload; static;
class function Decrement(var location: Int64) : Int64; overload; static;
class function Exchange(var location1: Integer; value: Integer)
: Integer; overload; static;
class function Exchange(var location1: Single;
value: Single) : Single; overload; static;
class function Exchange(var location1 : System.Object;
value : System.Object) : System.Object; overload; static;
class function CompareExchange(var location1: Integer;
value: Integer;
comparand: Integer) : Integer; overload; static;
class function CompareExchange(var location1: Single;
value: Single;
comparand: Single) : Single; overload; static;
class function CompareExchange(var location1 : System.Object;
value : System.Object;
comparand : System.Object) : System.Object; overload; static;
end;
Notice that all methods are class methods, so an instance of this class is never needed. Each distinct operation is guaranteed to finish execution once it begins.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Events >>
More .NET Articles
More By Xavier Pacheco