Skip to Main Content
C# in a Nutshell
book

C# in a Nutshell

by Ben Albahari, Ted Neward, Peter Drayton
March 2002
Intermediate to advanced content levelIntermediate to advanced
864 pages
31h 8m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell

Name

ReaderWriterLock

Synopsis

This class defines a lock that allows multiple readers, but only one writer. A thread can acquire a lock by calling AcquireReaderLock() or AcquireWriterLock(). ReleaseReaderLock() and ReleaseWriterLock() release the specific locks. Calling ReleaseReaderLock() on a writer lock releases both the writer lock and the reader lock. However, calling ReleaseWriterLock() on a reader lock throws a System.ApplicationException.

ReleaseLock() causes any lock to be released, but it returns a LockCookie, which represents the type of lock that RestoreLock() can use to obtain the same lock. UpgradeToWriterLock() upgrades a reader lock to a writer lock, and returns a LockCookie representing the original reader lock. Pass that cookie to DowngradeFromWriterLock() to restore the original reader lock.

public sealed class ReaderWriterLock {
// Public Constructors
   public method ReaderWriterLock();  
// Public Instance Properties
   public field bool IsReaderLockHeld{get; } 
   public field bool IsWriterLockHeld{get; } 
   public field int WriterSeqNum{get; } 
// Public Instance Methods
   public method void AcquireReaderLock(
        int millisecondsTimeout);  
   public method void AcquireReaderLock(TimeSpan timeout);  
   public method void AcquireWriterLock(
        int millisecondsTimeout);  
   public method void AcquireWriterLock(TimeSpan timeout);  
   public method bool AnyWritersSince(int seqNum);  
   public method void DowngradeFromWriterLock(
        ref LockCookie lockCookie);  
   public method LockCookie ReleaseLock(); public ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# 8.0 in a Nutshell

C# 8.0 in a Nutshell

Joseph Albahari, Eric Johannsen
C# 10 in a Nutshell

C# 10 in a Nutshell

Joseph Albahari
C# in a Nutshell, Second Edition

C# in a Nutshell, Second Edition

Peter Drayton, Ben Albahari, Ted Neward
Code like a Pro in C#

Code like a Pro in C#

Jort Rodenburg

Publisher Resources

ISBN: 0596001819Catalog PageErrata