Hosea146
Hosea146

Reputation: 7702

ReaderWriterLockSlim - Not sure how this works

I have the following declared on a Singleton type object named CicApplication:

internal static List<Fcda> FcdaCache
{
   get
   {
      // If the current thread already has a write lock, no need to attempt to acquire a read lock (which would fail anyway)
      if (CoaterDataLock.IsWriteLockHeld)
         return _fcdaCache;

      CoaterDataLock.EnterReadLock();
      try
      {
         return _fcdaCache;
      }
      finally
      {
         CoaterDataLock.ExitReadLock();
      }
   }
}

'CoaterDataLock' is declared as ReaderWriterLockSlim object

Elsewhere in my code, I perform the following query on 'FcdaCache':

CicApplication.FcdaCache.Where(row => row.Coater == coater)

My question is this. When I perform this query, will an attempt be made to get a read lock on FcdaCache? I assume it will but was not sure.

Upvotes: 0

Views: 254

Answers (1)

Fischermaen
Fischermaen

Reputation: 12458

I think your code doesn't lock anything. You enter the lock, return the _fcdaCache and leave the lock. After that you make a .Where(...) on that cache, which is then unlocked, so concurrency exceptions may occur or data races may happen.

Upvotes: 5

Related Questions