Using the same lock for multiple methods

前端 未结 5 1636
时光说笑
时光说笑 2021-01-01 13:07

I haven\'t had any issues using the same lock for multiple methods so far, but I\'m wondering if the following code might actually have issues (performance?) that I\'m not a

相关标签:
5条回答
  • 2021-01-01 13:47

    If the methods are unrelated as you state, then use a different lock for each one; otherwise it's inefficient (since there's no reason for different methods to lock on the same object, as they could safely execute concurrently).

    Also, it seems that these are instance methods locking on a static object -- was that intended? I have a feeling that's a bug; instance methods should (usually) only lock on instance fields.

    Regarding the Singleton design pattern:

    While locking can be safe for those, better practice is doing a delayed initialization of a field like this:

    private static object sharedInstance;
    public static object SharedInstance
    {
         get
         {
              if (sharedInstance == null)
                  Interlocked.CompareExchange(ref sharedInstance, new object(), null);
              return sharedInstance;
         }
    }
    

    This way it's a little bit faster (both because interlocked methods are faster, and because the initialization is delayed), but still thread-safe.

    0 讨论(0)
  • 2021-01-01 13:50

    To create a thread-safe singleton, use this technique.
    You don't need a lock.

    In general, each lock should be used as little as possible.
    The more methods lock on the same thing, the mroe likely you are to end up waiting for it when you don't really need to.

    0 讨论(0)
  • 2021-01-01 13:57

    By using the same object to lock on in all of those methods, you are serializing all access to code in all of the threads.

    That is... code running GetValue1() will block other code in a different thread from running GetValue2() until it's done. If you add even more code that locks on the same object instance, you'll end up with effectively a single-threaded application at some point.

    0 讨论(0)
  • 2021-01-01 14:00

    Good question. There are pros and cons of making locks more fine grained vs more coarse grained, with one extreme being a separate lock for each piece of data and the other extreme being one lock for the entire program. As other posts point out, the disadvantage of reusing the same locks is in general you may get less concurrency (though it depends on the case, you may not get less concurrency).

    However, the disadvantage of using more locks is in general you make deadlock more likely. There are more ways to get deadlocks the more locks you have involved. For example, acquiring two locks at the same time in separate threads but in the opposite order is a potential deadlock which wouldn't happen if only one lock were involved. Of course sometimes you may fix a deadlock by breaking one lock into two, but usually fewer locks means fewer deadlocks. There's also added code complexity of having more locks.

    In general these two factors need to be balanced. It's common to use one lock per class for convenience if it doesn't cause any concurrency issues. In fact, doing so is a design pattern called a monitor.

    I would say the best practice is to favor fewer locks for code simplicity's sake and make additional locks if there's a good reason (such as concurrency, or a case where it's more simple or fixes a deadlock).

    0 讨论(0)
  • 2021-01-01 14:05

    Shared lock locks other non-related calls

    If you use the same lock then locking in one method unnecessarily locks others as well. If they're not related at all than this is a problem since they have to wait for each other. Which they shouldn't.

    Bottleneck

    This may pose a bottleneck when these methods are frequently called. With separate locks they would run independently, but sharing the same lock it means they must wait for the lock to be released more often as required (actually three times more often).

    0 讨论(0)
提交回复
热议问题