0

ReaderWriterLockSlim allows a lock that is used to manage access to a resource, allowing multiple threads for reading or exclusive access for writing. currently we have the following code in place:

public ReaderWriterLockSlim protDataA = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
        public ReaderWriterLockSlim protDataB = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion);
        public void AMethod(ClassA Aobject )
        {
            try
            {
                protDataA.EnterWriteLock();
                //change A-typed-object
            }
            finally
            { protDataA.ExitWriteLock(); }
        }
        public void BMethod(ClassB Bobject)
        {
            try
            {
                protDataB.EnterWriteLock();
                //change B-typed-object
            }
            finally
            { protDataB.ExitWriteLock(); }
        }

Now, If I need to protect data for both A and B typed objects what should be the approach and Why ?

//Approach-1 - Making multiple locks on a single block
public void ABMethod(ClassA Aobject, ClassB Bobject)
            {
                try
                {
                    protDataA.EnterWriteLock();
                    protDataB.EnterWriteLock();
                    //change A-typed-object
                    //change B-typed-object
                }
                finally
                {
                    protDataA.ExitWriteLock();
                    protDataB.ExitWriteLock();
                }
            }

    //Approach-2 - Or Making a single lock would serve the same purpose ?
            public void ABMethod(ClassA Aobject, ClassB Bobject)
            {
                try
                {
                    protDataA.EnterWriteLock();
                    //change A-typed-object
                    //change B-typed-object
                }
                finally
                {
                    protDataA.ExitWriteLock();
                }
            }
  • 1
    If I call `BMethod` and the 2nd approach of `ABMethod` what stops both from writing at the same time? Also do you really need to have both locks out at the same time, can you take one do what you need then take the other? – Scott Chamberlain Jul 20 '15 at 16:22
  • @ScottChamberlain , would you suggest replacing the two locking objects protDataA and protDataB with a single locking object in all the 3 methods(AMethod, BMethod, ABMethod) ? The thread will wait for access to B object even if the A object is changing. – LetsKickSomeAss in .net Jul 20 '15 at 16:42

1 Answers1

1

Both approaches are good. Choosing the best one requires knowledge about other usages of the locks: how often they are locked, how long, etc.

Until there is a certain perfomance bottleneck (found via profiling), the second(single lock) approach is preferred: it never suffers from deadlock, so you have lower chance to make bugged code.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153