System.Threading.ReaderWriterLock Class

Assembly: Mscorlib.dll
Namespace: System.Threading
Summary
Defines the lock that implements single-writer and multiple-reader semantics.
C# Syntax:
public sealed class ReaderWriterLock
Remarks
ReaderWriterLock supports the following features:

Timeout is in milliseconds. The following table shows the possible timeout values.



Value Description
-1 Infinite.
0 No timeout.
>0 The number of milliseconds to wait.

Negative numbers are interpreted as unsigned, with the exception of -1, which has special meaning.

See also:
System.Threading Namespace

System.Threading.ReaderWriterLock Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the ReaderWriterLock class.
Public Properties
IsReaderLockHeld Read-only

Gets a value indicating whether the current thread holds the reader lock.
IsWriterLockHeld Read-only

Gets a value indicating whether the current thread holds the writer lock.
WriterSeqNum Read-only

Gets the current sequence number.
Public Methods
AcquireReaderLock Overloaded:
AcquireReaderLock(int millisecondsTimeout)

Acquires reader lock, using an Int32 value for the timeout.
AcquireReaderLock Overloaded:
AcquireReaderLock(TimeSpan timeout)

Acquires the reader lock, using a TimeSpan value for the timeout.
AcquireWriterLock Overloaded:
AcquireWriterLock(int millisecondsTimeout)

Acquires the writer lock, using an Int32 value for the timeout.
AcquireWriterLock Overloaded:
AcquireWriterLock(TimeSpan timeout)

Acquires the writer lock, using a TimeSpan value for the timeout.
AnyWritersSince Indicates whether intermediate writes have occurred since the sequence number was obtained.
DowngradeFromWriterLock Restores the lock status of the thread to when it calls ReaderWriterLock.UpgradeToWriterLock.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ReleaseLock Releases the lock, regardless of the number of times the thread acquired the lock.
ReleaseReaderLock Releases the reader lock.
ReleaseWriterLock Releases the writer lock.
RestoreLock Restores the lock status of the thread to what it was before calling ReaderWriterLock.ReleaseLock.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
UpgradeToWriterLock Overloaded:
UpgradeToWriterLock(int millisecondsTimeout)

Upgrades the reader thread to a writer, using an Int32 value for the timeout.
UpgradeToWriterLock Overloaded:
UpgradeToWriterLock(TimeSpan timeout)

Upgrades the reader thread to a writer, using a TimeSpan value for the timeout.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Threading.ReaderWriterLock Member Details

ctor #1
Summary
Initializes a new instance of the ReaderWriterLock class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public ReaderWriterLock();

Return to top


Property: IsReaderLockHeld (read-only)
Summary
Gets a value indicating whether the current thread holds the reader lock.
C# Syntax:
public bool IsReaderLockHeld {get;}

Return to top


Property: IsWriterLockHeld (read-only)
Summary
Gets a value indicating whether the current thread holds the writer lock.
C# Syntax:
public bool IsWriterLockHeld {get;}

Return to top


Property: WriterSeqNum (read-only)
Summary
Gets the current sequence number.
C# Syntax:
public int WriterSeqNum {get;}
Remarks
The caller should be a reader or writer to get meaningful results.

Return to top


Overloaded Method: AcquireReaderLock(
   int millisecondsTimeout
)
Summary
Acquires reader lock, using an Int32 value for the timeout.
C# Syntax:
public void AcquireReaderLock(
   int millisecondsTimeout
);
Parameters:

millisecondsTimeout

The timeout in milliseconds.

Remarks
ReaderWriterLock.AcquireReaderLock supports nested reader locks. The thread blocks if a different thread has the writer lock.

Return to top


Overloaded Method: AcquireReaderLock(
   TimeSpan timeout
)
Summary
Acquires the reader lock, using a TimeSpan value for the timeout.
C# Syntax:
public void AcquireReaderLock(
   TimeSpan timeout
);
Parameters:

timeout

A TimeSpan specifying the timeout period.

Remarks
ReaderWriterLock.AcquireReaderLock supports nested reader locks. The thread blocks if a different thread has the writer lock.

Return to top


Overloaded Method: AcquireWriterLock(
   int millisecondsTimeout
)
Summary
Acquires the writer lock, using an Int32 value for the timeout.
C# Syntax:
public void AcquireWriterLock(
   int millisecondsTimeout
);
Parameters:

millisecondsTimeout

The timeout in milliseconds.

Remarks
ReaderWriterLock.AcquireWriterLock supports nested writer locks. The thread blocks if a different thread has the reader lock, and deadlocks if it has the reader lock. Use ReaderWriterLock.UpgradeToWriterLock if you are not sure that the thread has the reader lock. This will upgrade the reader thread to a writer.

Return to top


Overloaded Method: AcquireWriterLock(
   TimeSpan timeout
)
Summary
Acquires the writer lock, using a TimeSpan value for the timeout.
C# Syntax:
public void AcquireWriterLock(
   TimeSpan timeout
);
Parameters:

timeout

The TimeSpan specifying the timeout period.

Remarks
ReaderWriterLock.AcquireWriterLock supports nested writer locks. The thread blocks if a different thread has the reader lock, and deadlocks if it has the reader lock. Use ReaderWriterLock.UpgradeToWriterLock if you are not sure that the thread has the reader lock. This will upgrade the reader thread to a writer.

Return to top


Method: AnyWritersSince(
   int seqNum
)
Summary
Indicates whether intermediate writes have occurred since the sequence number was obtained.
C# Syntax:
public bool AnyWritersSince(
   int seqNum
);
Parameters:

seqNum

The sequence number.

Return Value:
true if intermediate writes have occurred since the sequence number was obtained; otherwise, false.
Remarks
The caller should be a reader or writer to get meaningful results.

Return to top


Method: DowngradeFromWriterLock(
   ref LockCookie lockCookie
)
Summary
Restores the lock status of the thread to when it calls ReaderWriterLock.UpgradeToWriterLock.
C# Syntax:
public void DowngradeFromWriterLock(
   ref LockCookie lockCookie
);
Parameters:

lockCookie

The LockCookie.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~ReaderWriterLock();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ReleaseLock()
Summary
Releases the lock, regardless of the number of times the thread acquired the lock.
C# Syntax:
public LockCookie ReleaseLock();
Return Value:
A LockCookie value representing the released lock.

Return to top


Method: ReleaseReaderLock()
Summary
Releases the reader lock.
C# Syntax:
public void ReleaseReaderLock();
Remarks
If the current thread is holding the writer lock, calling ReaderWriterLock.ReleaseReaderLock releases the writer lock as well as the reader lock.

Return to top


Method: ReleaseWriterLock()
Summary
Releases the writer lock.
C# Syntax:
public void ReleaseWriterLock();
Remarks
ReaderWriterLock.ReleaseWriterLock removes the thread as a writer if this is not a nested call to release the lock. If the current thread is holding the reader lock, calling ReaderWriterLock.ReleaseWriterLock throws an ApplicationException.

Return to top


Method: RestoreLock(
   ref LockCookie lockCookie
)
Summary
Restores the lock status of the thread to what it was before calling ReaderWriterLock.ReleaseLock.
C# Syntax:
public void RestoreLock(
   ref LockCookie lockCookie
);
Parameters:

lockCookie

The LockCookie.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Overloaded Method: UpgradeToWriterLock(
   int millisecondsTimeout
)
Summary
Upgrades the reader thread to a writer, using an Int32 value for the timeout.
C# Syntax:
public LockCookie UpgradeToWriterLock(
   int millisecondsTimeout
);
Parameters:

millisecondsTimeout

The timeout in milliseconds.

Return Value:
A LockCookie value.

Return to top


Overloaded Method: UpgradeToWriterLock(
   TimeSpan timeout
)
Summary
Upgrades the reader thread to a writer, using a TimeSpan value for the timeout.
C# Syntax:
public LockCookie UpgradeToWriterLock(
   TimeSpan timeout
);
Parameters:

timeout

The TimeSpan specifying the timeout period.

Return Value:
A LockCookie value.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.