Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi everybody,

Is there any way to synchronize two methods? Example:

public class myclass
{
     private int x;
     public void method1()
     {
          //Access to x from thread1
     }

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


There for, method1() and method2() will access to x from other threads. Is there any way to synchronize them (just one method can access variable x at a time)?
Posted
Updated 16-May-11 21:04pm
v2
Comments
Sergey Alexandrovich Kryukov 17-May-11 3:14am    
You synchronize threads, not methods. Any method can be called in any thread. I suggest you fix the title of the question.
--SA

1 solution

Yes. Look at the Lock[^] statement - that is exactly what it is there for.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-May-11 3:13am    
Correct, it's a 5.
--SA
Kim Togo 17-May-11 6:29am    
My 5.

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