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);
}
}
}
In that context, it isn’t; however, since locks are re-entrant that can be useful to ensure that any other caller of
AddToSubscribersobserves the lock. Actually, for that reason I’d say “remove it fromSubscribeand just letAddToSubscribersdo the locking”.However! A lock on a
Typeis pretty dangerous. A field would be safer:and
lock(syncLock). Depending on whensubscribersis assigned, you might also get away withlock(subscribers)(and no extra field).I should also note that having an instance method add to static state is pretty…. unusual; IMO
Subscribeshould be astaticmethod, since it has nothing to do with the current instance.