IsReaderLockHeld Property
Collapse the table of content
Expand the table of content

ReaderWriterLock.IsReaderLockHeld Property

 

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

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public bool IsReaderLockHeld { get; }

Property Value

Type: System.Boolean

true if the current thread holds a reader lock; otherwise, false.

The following code example demonstrates how to use IsReaderLockHeld to avoid deadlocks.

using System;
using System.Threading;

class LockHeld
{
    static void Main()
    {
        ReaderWriterLock rwLock = new ReaderWriterLock();

        rwLock.AcquireWriterLock(Timeout.Infinite);
        rwLock.AcquireReaderLock(Timeout.Infinite);

        if(rwLock.IsReaderLockHeld)
        {
            Console.WriteLine("Reader lock held.");
            rwLock.ReleaseReaderLock();
        }
        else if(rwLock.IsWriterLockHeld)
        {
            Console.WriteLine("Writer lock held.");
            rwLock.ReleaseWriterLock();
        }
        else
        {
            Console.WriteLine("No locks held.");
        }

        if(rwLock.IsReaderLockHeld)
        {
            Console.WriteLine("Reader lock held.");
            rwLock.ReleaseReaderLock();
        }
        else if(rwLock.IsWriterLockHeld)
        {
            Console.WriteLine("Writer lock held.");
            rwLock.ReleaseWriterLock();
        }
        else
        {
            Console.WriteLine("No locks held.");
        }
    }
}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft