Use ReaderWriter Lock in C#


ReaderWriterLock synchronize access to a resource.

A ReaderWriterLock has better throughput than a Monitor, which is one-at-a-time lock. This works for a situation when a resource is changed rarely.

Let us see how to declare a ReaderWriter lock in C# −

static ReaderWriterLock r = new ReaderWriterLock();

The following are the properties of a ReaderWriter lock in C# −

Sr.No. Property & Description
1

IsReaderLockHeld

Gets a value indicating whether the current thread holds a reader lock.

2

IsWriterLockHeld

Gets a value indicating whether the current thread holds a Writer lock.

3

WriterSeqNum

Current sequence number

The following are some of the methods of ReaderWriter lock −

Sr.No. Method & Description
1

AcquireReaderLock(TimeSpan)

Acquires a reader lock, using a TimeSpan value for the time-out.

2

AcquireWriterLock(TimeSpan)

Acquires a Writer lock, using a TimeSpan value for the time-out.

3

Equals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

4

GetHashCode()

Default hash

5

GetType()

Type of the current instance

6

ReleaseReaderLock()

Lock Count decrements

Updated on: 30-Jul-2019

481 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements