Do something else if ReadWriteSlimlock is held

Posted by user43838 on Stack Overflow See other posts from Stack Overflow or by user43838
Published on 2010-04-01T14:06:30Z Indexed on 2010/04/01 14:13 UTC
Read the original article Hit count: 238

Filed under:
|
|

Hi everyone,

I have implemented ReaderWriterLockSlim, Now i don't want it to wait at the lock. I want to do something else if the lock is held.

I considered using is isWriterLockHeld but it does not makes much sense to me, Since if two threads come in at the same time and enter the if statement at the same time one will still be waiting at the lock

here is my code.

                   ReaderWriterLockSlim rw = GetLoadingLock(parameters);
                   rw = GetLoadingLock(parameters);
                   try
                   {
                      rw.EnterWriteLock();
                      item = this.retrieveCacheItem(parameters.ToString(), false);
                      if (item != null)
                      {
                          parameters.DataCameFromCache = true;

                          // if the data was found in the cache, return it immediately
                          return item.data;                              
                      }
                      else
                      {
                          try
                          {
                              object loaditem = null;
                              itemsLoading[parameters.ToString()] = true;
                              loaditem = this.retrieveDataFromStore(parameters);
                              return loaditem;
                          }
                          finally {
                             itemsLoading.Remove(parameters.ToString());
                          }
                      }
                   }
                   finally
                   {
                      rw.ExitWriteLock();
                   }

Can anyone please guide me in the right direction with this.

Thanks

© Stack Overflow or respective owner

Related posts about c#

Related posts about multithreading