I am trying to fully understand how creating separate threads that call the same method of a class instance can effect local variables in the method.
For example I have a class with a single method (Divide)
public class Maths
{
public int Num1;
public int Num2;
public void Divide()
{
for (long i = 0; i < 100000; i++)
{
Num1 = 2;
Num2 = 2;
int result = Num1 / Num2;
Num1 = 0;
Num2 = 0;
}
}
}
Two threads are instantiated and the divide method called as follows:
static void Main(string[] args)
{
Maths m = new Maths();
Task t1 = new Task(() => m.Divide());
Task t2 = new Task(() => m.Divide());
List<Task> tl = new List<Task> { t1, t2 };
Parallel.ForEach(tl, task => task.Start());
Console.ReadLine();
}
}
Sometimes this code runs ok. But other times it throws a dividebyzero error on the line:
int result = Num1 / Num2;
My assumption is that one of the threads is resetting the Num1 and Num2 to zero just before the other thread calls the Num1 / Num2. Therefore causing a divide by zero exception.
This would make sense and I should use a lock but I don't understand how these local variables Num1 and Num2 are shared between the threads because my understanding was that local variables are not shared between threads?