Piyush Joshi wrote:
So first Thread1 reads value of id =1.
then Thread2 also reads id = 1.
Thread1 calculates the expression (1-1) and assigns id=0;
Thread2 calculates the expression (1-1) and assigns id=0;
so now both threads see value of id=0;
Let me clarify myself:
I think id = 1 - id; will be calculated by making a temporary variable in memory and storing value of id:
somewhere in memory: so id = 1 - id; will be broken down to this:
int temp = id;
id = 1 - temp;
so what I said before read it like this:
So first Thread1 reads value of id =1 and stores it in a temporary int temp1 = 1;.
then Thread2 also reads id = 1 and stores it in a temporary int temp2 = 1;.
Thread1 calculates the expression (1-temp1) and assigns id=0;
Thread2 calculates the expression (1-temp2) and assigns id=0;
This is what I actually wanted to ask. Sorry for misunderstanding....
is there something wrong in my analysis?