I would like to understand how exactly the race condition works. Specifically on this example. The result of this program is max value i.e 200 000 or less than this e.g 150 000. So my question is when it stops counting when result is less than 200 000, how it works, and how it looks like step by step. I think if i can understand this on that example it can helps me understand general idea about multithreading. Thanks in advance!
using System;
using System.Threading;
class Kontekst
{
public double x = 0.0;
};
class Watek
{
public Kontekst kon;
public int num;
public Watek(Kontekst kon_, int num_)
{
kon = kon_;
num = num_;
}
public void Dzialanie()
{
Console.WriteLine("Watek " + num);
for (int i = 0; i < 100000; ++i) kon.x += 1.0;
}
};
public class SemaforyPrzyklad
{
public static void Main(string[] args)
{
Kontekst kon = new Kontekst();
Watek w1 = new Watek(kon, 1);
Watek w2 = new Watek(kon, 2);
Thread watek1 = new Thread(w1.Dzialanie);
Thread watek2 = new Thread(w2.Dzialanie);
watek1.Start();
watek2.Start();
watek1.Join();
watek2.Join();
Console.WriteLine("x = " + kon.x);
Console.ReadKey();
}
}