Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:24:10+00:00 2026-05-31T22:24:10+00:00

I have a class using ReaderWriterLockSlim with a read method and a write method

  • 0

I have a class using ReaderWriterLockSlim with a read method and a write method that uses the read method to retrieve the element to be modified. A quick example would be:

class FooLocker
{
    ReaderWriterLockSlim locker = new ReaderWriterLockSlim();
    List<Foo> fooList = new List<Foo>();

    public void ChangeFoo(int index, string bar)
    {
        locker.EnterWriteLock();

        try
        {
            Foo foo = GetFoo(index);
            foo.Bar = bar;
        }
        finally
        {
            locker.ExitWriteLock();
        }
    }

    public Foo GetFoo(int index) 
    {
        locker.EnterReadLock(); //throws System.Threading.LockRecursionException

        try
        {
            return fooList[index];
        }
        finally
        {
            locker.ExitReadLock();
        }
    }

    //snipped code for adding instances etc.
}

As above, this code throws a LockRecursionException when calling ChangeFoo() because a write lock is already held when GetFoo() tries to enter a read lock.

I’ve checked the documentation for ReaderWriterLockSlim and I can use LockRecursionPolicy.SupportsRecursion to allow the above to work. However, the documentation also recommends that this shouldn’t be used for any new development and should only be used when upgrading existing code.

Given this, what is the best practice to achieve the same result where a write method can use a read-only method to retrieve the thing that needs to be modified?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:24:11+00:00Added an answer on May 31, 2026 at 10:24 pm

    You can divide your class up into exposed methods and private inner methods. The inner methods do the logic like fetching and the public methods perform the locking. Example:

    class FooLocker 
    { 
        ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); 
        List<Foo> fooList = new List<Foo>(); 
    
    
        public void ChangeFoo(int index, string bar) 
        { 
            locker.EnterWriteLock(); 
    
            try 
            { 
                Foo foo = UnsafeGetFoo(index); 
                foo.Bar = bar; 
            } 
            finally 
            { 
                locker.ExitWriteLock(); 
            } 
        } 
    
        public Foo GetFoo(int index)  
        { 
            locker.EnterReadLock();  
    
            try 
            { 
                return UnsafeGetFoo(index);
            } 
            finally 
            { 
                locker.ExitReadLock(); 
            } 
        } 
    
        private Foo UnsafeGetFoo(int index)
        {
            return fooList[index]; 
        }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that uses a method from a bean. I'm trying to
How to select next n hidden elements that have class foo using jQuery?
I have an application that opens another class using intent : private void createRepository(){
I have a class that I am using below. And I am using this
I have a class that solves an equation using an approximation, evaluates the approximation
I have a product class in C# that inherits from another product class using
I have a class that I am creating within an AppDomain using CreateInstanceAndUnwrap. The
I currently have a class that uses the KeyValuePair with List to store a
If I am using ReaderWriterLockSlim to acquire read/write locks, do I need to make
I have rather class using TcpClient that spins of a Thread doing while (!streamReader.EndOfStream)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.