Share via


Can lock work between multiple objects of a class ?

Question

Tuesday, May 17, 2011 6:51 PM

I have an application, which creates multiple objects, one for each request. Each object would have a thread associated with it (using ThreadPool).

public void ProcessInputRequest(object source, FileSystemEventArgs e)
{
 for (int i = 0; i < m_numThreads; i++)
 {
  if (urlFinderMonitorLibArray[i].m_isReqComplete)
  {
   urlFinderMonitorLibArray[i].m_isReqComplete = false; 
   ThreadPool.QueueUserWorkItem(urlFinderMonitorLibArray[i].ProcessRequest,m_inputFileLocation);
  }
 } 
}

Now, there is a function inside my class urlFinderMonitorLibArray, which calls another application and that application cannot be shared between multiple threads (one for each object as indicated above). So, I want to lock that piece of code in the function. For example
 
class urlFinderMonitorLibArray
{
 public void A{}
 {
  lock(this)
  {
   //Call application X
  }
 }

 public void B{}
 {
  lock(this)
  {
   //Call application Y
  }
 } 
}

I want to lock methods A and B, so that only thread of one object has control over it. I know that lock would work for multiple threads of the same object, but I am not sure if scope of lock is across multiple objects or just one object. Would the above code work ? 

All replies (5)

Tuesday, May 17, 2011 8:52 PM ✅Answered | 1 vote

lock(this) uses your current instance as the reference for the lock.  Therefore, different objects would not be locked.

What might work, and I'm not sure because I've never tried it myself is if you create a static lock object. 

On edit:  Actually, what I mean to say is that I've never used one for this purpose before (usually, I use a static lock object to perform synchronization in static class methods, but never to govern locks between instances, although I don't believe anything prevents it from working this way).

To do this, add the following to this class:

private static object m_lockObject = new object();

This will create a shared object that all instances of your class can access.  Then, rather than use lock(this), use lock(m_lockObject).  The compiler doesn't seem to be bothered by the fact that you are now using a static object reference to lock on, so it might actually do the trick.


Tuesday, May 17, 2011 9:42 PM ✅Answered | 2 votes

That won't work because the lock is only for the supplied instance (this) and as CS001 already mentioned, one of the solutions is to create an static object so all the instances will refer to the same lock.

An alternative is to use Mutex, which can acquire a system-wide lock, that would work even if you have several process of the same application.

Regards,

Fábio

"To alcohol! The cause of and solution to all of life's problems." - Homer Simpson


Thursday, May 19, 2011 9:56 AM ✅Answered | 2 votes

A named Mutex is unique to the system. The C# object will be local, but the locking mechanism is not. It's like a FileInfo: the FileInfo object may be local, the file it represents is not.


Thursday, May 19, 2011 1:16 AM

Thanks CS001 / Fabio.

I thought of using static object, but just wanted to know the behavior of this lock. What I implemented currently: I created lock and passed it as an argument to the constructor. The constructor assigns this lock to its local object. I think this will do the trick.

Fabio, why should a Mutex work ? Correct me if I am wrong, but my understanding is that if I a create a Mutex in urlFinderMonitorLibArray class, it is local to that class, isn't it ?


Thursday, May 19, 2011 5:54 PM

awesome, that makes things simpler !!! thanks Louis !