Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rationale behind lock inside lock?

I am reviewing an example code in a book and came across the following code(simplified). In the code, when Subscribe(T subscriber) is called, the thread enters into a lock section. and then, when code inside the lock calls AddToSubscribers(T subscriber) method, the method has another lock. why is this second lock necessary?

public abstract class SubscriptionManager<T> where T : class 
{
   private static List<T> subscribers;
   private static void AddToSubscribers(T subscriber)
   {
      lock (typeof(SubscriptionManager<T>))
      {
         if (subscribers.Contains(subscriber))
            return;
         subscribers.Add(subscriber);
      }
   }

   public void Subscribe(T subscriber)
   {
      lock (typeof(SubscriptionManager<T>))
      {
         AddToSubscribers(subscriber);
      }
   }
}
like image 666
Yeonho Avatar asked Dec 21 '11 07:12

Yeonho


People also ask

What is the purpose of lock in period?

Lock-in period refers to the number of years in which investors cannot withdraw or sell the funds they have created. Once the lock-in period is over, the investor must not withdraw the funds immediately rather they should observe the performance of the funds.

What is lock in period mortgage?

A lock period refers to an amount of time during which a mortgage lender must guarantee a specific interest rate or other loan terms open to a borrower. This period of time is typically 30 or 90 days, but will vary based on the lender and on the borrower's underwriting.

What is a locked in contract?

Related Content. An agreement that the parties will lock themselves into negotiations to negotiate in good faith, sometimes subject to a time limit.

What is lock penalty?

What Is The Meaning Of A Lock In Period? This term defines a period of time in which you'll have to pay a penalty, if you somehow wish to end your home loan earlier than agreed. That can mean you want to pay off the loan in full, refinance your loan, or even sell your property.


2 Answers

In that context, it isn't; however, since locks are re-entrant that can be useful to ensure that any other caller of AddToSubscribers observes the lock. Actually, for that reason I'd say "remove it from Subscribe and just let AddToSubscribers do the locking".

However! A lock on a Type is pretty dangerous. A field would be safer:

// assuming static is correct
private static readonly object syncLock = new object();

and lock(syncLock). Depending on when subscribers is assigned, you might also get away with lock(subscribers) (and no extra field).

I should also note that having an instance method add to static state is pretty.... unusual; IMO Subscribe should be a static method, since it has nothing to do with the current instance.

like image 96
Marc Gravell Avatar answered Sep 23 '22 08:09

Marc Gravell


In the code you posted, it isn't necessary. But then again, the code you posted is incomplete - for example the subscribers list is never initialized.

Locking on typeof(SubscriptionManager) is probably not a good idea either - locking on the subscribers field would be better - but would require the subscribers field to be initialized, e.g.

private static List<T> subscribers = new List<T>();
like image 41
Joe Avatar answered Sep 22 '22 08:09

Joe