How come the following doesn't give me a read / write error ever? In my main thread I increment i, which is a read, and a write. In both threads I write to total. I would expect to see an error, but I leave it running and never get one, why is this?
For reference, my computer has 2 cores, 4 logical cores, its an Intel i3.
using System;
using System.Collections.Generic;
using System.Threading;
namespace Threading001
{
class Program
{
static void Main(string[] args)
{
int i = 0;
int total = 0;
new Thread(() => {
var service2 = new Service();
while (true)
{
try
{
total += i;
}
catch (Exception ex)
{
Console.WriteLine("Error from thread 2: {0}", ex);
break;
}
}
}).Start();
while (true)
{
try
{
total += i;
i++;
}
catch (Exception ex)
{
Console.WriteLine("Error from thread 1: {0}", ex);
break;
}
}
}
}
}