T
Tony Johansson
Hi!
At the bottom is a simple thread program.
If I run this program on a computer that doesn't have two cores I always
get 100010 which is correct.
If I run this program on a computer that have two cores I sometimes get a
value that could be 96542 or 97129 or something else that less that 100010.
This is because of not doing for example lock or interlocked in this
example.
So my question is why do I always get the correct result when I run the
program on a computer with only one core ??
This computer with only one core is very old perhaps 10 years.
Class Test
{
static void UpdateCount()
{
for (int x = 0; x <= 10000; x++)
Counter.count = Counter.count + 1;
}
public static void Main()
{
ThreadStart starter = new ThreadStart(UpdateCount);
Thread[] threads = new Thread[10];
for (int x = 0; x < 10; x++)
{
threads[x] = new Thread(starter);
threads[x].Start();
}
for (int x = 0; x < 10; x++)
{
threads[x].Join();
}
Console.WriteLine("Total: {0}", Counter.count);
Console.ReadLine();
}
}
//Tony
At the bottom is a simple thread program.
If I run this program on a computer that doesn't have two cores I always
get 100010 which is correct.
If I run this program on a computer that have two cores I sometimes get a
value that could be 96542 or 97129 or something else that less that 100010.
This is because of not doing for example lock or interlocked in this
example.
So my question is why do I always get the correct result when I run the
program on a computer with only one core ??
This computer with only one core is very old perhaps 10 years.
Class Test
{
static void UpdateCount()
{
for (int x = 0; x <= 10000; x++)
Counter.count = Counter.count + 1;
}
public static void Main()
{
ThreadStart starter = new ThreadStart(UpdateCount);
Thread[] threads = new Thread[10];
for (int x = 0; x < 10; x++)
{
threads[x] = new Thread(starter);
threads[x].Start();
}
for (int x = 0; x < 10; x++)
{
threads[x].Join();
}
Console.WriteLine("Total: {0}", Counter.count);
Console.ReadLine();
}
}
//Tony