Click here to Skip to main content
15,905,877 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Ex, i've a class like this:
public class myclass
{
      private Object x;
      public void method1()
      {
          lock(x)
          {
                //access to x from thread1
          }
      }
      
      public void method2()
      {
             //access to x from thread2
      }
}


Now, if method1() is called from a thread(but not completed yet). And method2() is called from an another thread(method1() hasn't completed yet from thread thread1). Will thread2 have to wait until method1() completed?
Posted
Updated 17-May-11 3:11am
v3
Comments
Sergey Alexandrovich Kryukov 17-May-11 19:01pm    
Don't re-post. Yes, I understand the question is a bit different; but you should have improved your previous question as it's too close.
--SA

No, the lock is only about x. The wait will occure if a thread is inside the lock block, and another thread is trying to enter the lock at the same time.

To make method2 block, you should write something like this:
C#
public void method1()
{
    lock(x)
    {
          //access to x from thread1
    }
}

public void method2()
{
    lock(x)
    {
        ...
    }
}


-------------------------------

But, my question is that if method2() hasn't got a lock statement, will it have to wait?

If there is no lock in method2, it will not wait.
Now about puting a lock or not, it depends what you do in method1 and method2.
If you put the lock in method1 to protect a few members, and you access the same members in method2, then you will probably have to use a lock in method2 too.
If you do something very different, then you don't need to lock in method2.
 
Share this answer
 
v4
Comments
ambarishtv 17-May-11 9:15am    
my5
Manfred Rudolf Bihy 17-May-11 9:22am    
Yep, my 5 too! :)
thanhvinh0906 17-May-11 9:26am    
But, my question is that if method2() hasn't got a lock statement, will it have to wait?
Sergey Alexandrovich Kryukov 17-May-11 19:02pm    
No!!!!!!!!!!!!
--SA
Sergey Alexandrovich Kryukov 17-May-11 19:02pm    
Correct, my 5.
--SA
No, the locking will have to be in both methods. Locking on an object keeps other threads from entering on a lock section on that object. The method doesn't have to finish as a whole, unless the lock spans the whole of the method body. As soon as one lock section is exited any other thread trying to enter a lock on the same object get a chance to enter their lock.

C#
public class myclass
{
      private Object x;
      public void method1()
      {
          lock(x)
          {
                //access to x from thread1
          }
      }

      public void method2()
      {
          lock(x)
          {
                //access to x from thread2
          }
      }
}



Regards,

-MRB
 
Share this answer
 
v3
Comments
Olivier Levrey 17-May-11 9:13am    
Great answer. You were faster than me. Have a 5.
Manfred Rudolf Bihy 17-May-11 9:23am    
Thank you!
ambarishtv 17-May-11 9:13am    
i agreed :) 5
Manfred Rudolf Bihy 17-May-11 9:23am    
Thanks!
thanhvinh0906 17-May-11 9:24am    
But, my question is that if method2() hasn't got a lock statement, will it have to wait?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900