.NET
  Home arrow .NET arrow Page 11 - Threading in Delphi for .NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Threading in Delphi for .NET
By: Xavier Pacheco
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 8
    2004-08-30

    Table of Contents:
  • Threading in Delphi for .NET
  • Threading
  • The System.Threading Namespace
  • Delegates in Delphi
  • Creating Threads Using Static Methods
  • Threading Priority
  • Apartment State and Thread Pooling Class
  • Timer Classes
  • Writing Thread-safe Code .NET Style
  • Mutex and Monitor Classes
  • Locks that Distinguish Between Readers and Writers
  • Events
  • Thread Local Storage
  • User Interface Issues
  • Threading Exceptions

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More .NET Articles
    More By Xavier Pacheco


     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek