Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have a little odd question!
I am writing an App. I have a class named X and I have an array of X. each member of array point to a different instant of X with different data. My X class has some functions and one of them for example Add.
My question is if 2 or more threads want to call Add in an instance of X what will happen?
I know I have to syncronize my function but I do not want to have a static lock in the class because other instances of X then will wait for signal, So I defined a non static object (Mutex).

1- Is it correct? Or there are some other solutions!
2- Can anyone please describe me what will happen when more than one thread want to call a same function in a same instance!

C++
public Class X
{
  private object _Lock=new object();
  private object Var1;
  private object Var2;
  // ...
  public void Add()
  {
    lock(_Lock)
    { 
       //I am doing something related to the current instance.
       // so other instances of X  do not need
       //to wait for each other. 
    }
  }
}
Posted

1 solution

1. Your code is correct. You can also instead of lock statement use Monitor class and its methods Enter, Leave and TryEnter which will allow a separate handling in the case that the object is locked.

2.
a. the first thread will lock the instance using the _Lock object
b. it now can safelly run the code between parenthesis below the lock statement.
c. new thread will enter the method. It will try lock, but it's imposible. So it will wait until the object is released by the first thread.
d. the first thread will leave lock statement section which will release the lock
e. one of waiting threads will be waken up and will lock the object
f. it will do its work. When it ends, other possible thread will again be waken up and able to lock...
 
Share this answer
 
Comments
aidin Tajadod 7-Sep-10 13:21pm    
@voloda2:
Thanks so much voloda2,
One more question! what will happen if I do not have lock! for example there are two threads that want to access a same function( so both try to access to the same memory). I wanted to know if OS or .Net Framework creates another instance of that method or something else?! ( It is a little confusing for me! ).
Thanks again,
Aidin
voloda2 8-Sep-10 14:35pm    
What do you mean by another instance of a method? It's a standard call. The difference is that you have same method accessed from two threads and two different stack.
If you will not modify shared data after you initialize shared object instance, you don't need synchronization (locking).
Main goal of synchronization is to keep objects in consistent and predictable state - image following situation:
1. you have an instance variable of value 3 (this.i == 3)
2. you read int variable from two threads into a local variable (lets say you have int i = this.i;)
3. you increment your local variable (i++);
4. you save new value back to instance member (this.i = i;)

I would expect value 5 but in this case you can quite easily get also value 4 (both threads may read value 3, increment and save back).
aidin Tajadod 13-Sep-10 1:52am    
Thank voloda2,
Actually my second question was not about synchronization, I was thinking how it is managed and I think I have got my answer.
Thanks for your help.

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