Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the same lock for multiple methods

Tags:

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 aware of:

private static readonly object lockObj = new object();  public int GetValue1(int index) {     lock(lockObj)     {         // Collection 1 read and/or write     } }  public int GetValue2(int index) {     lock(lockObj)     {         // Collection 2 read and/or write     } }  public int GetValue3(int index) {     lock(lockObj)     {         // Collection 3 read and/or write     } } 

The 3 methods and the collections are not related in anyway.

In addition, will it be a problem if this lockObj is also used by a singleton (in Instance property) ?

Edit: To clarify my question on using the same lock object in a Singleton class:

private static readonly object SyncObject = new object();  public static MySingleton Instance {     get     {         lock (SyncObject)         {           if (_instance == null)           {               _instance = new MySingleton();           }         }         return _instance;     } }  public int MyMethod() {       lock (SyncObject)       {            // Read or write       }   } 

Will this cause issues?

like image 491
alhazen Avatar asked Dec 23 '10 20:12

alhazen


People also ask

What is a synchronized lock?

When a thread invokes a synchronized method, it automatically acquires the intrinsic lock for that method's object and releases it when the method returns. The lock release occurs even if the return was caused by an uncaught exception.

Can multiple threads take a lock simultaneously?

Only one thread can hold a lock at a time. If a thread tries to take a lock that is already held by another thread, then it must wait until the lock is released.

Why must the lock key on an object be the same for each thread?

Object locks come into play only when there are synchronized methods. When an object has one or more synchronized methods, a thread can enter a synchronized method only if the thread can get the key to the object's lock!

How does lock work in multithreading?

A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can acquire the lock and everyone accesses to the shared resource requires that the lock be acquired first.


1 Answers

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.

like image 74
user541686 Avatar answered Sep 27 '22 19:09

user541686