.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT