I am trying to understand multi-threading and I have the following code and I need to ensure thread safety (typically without using lock statement) by getting the final result 10,000,000, but if you run the following code multiple time in VS you get different values close to 10,000,000 but never reaches it, so I need to fix this code without using lock statement.
using System;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
private static int _counter = 0;
private static void DoCount()
{
for (int i = 0; i < 100000; i++)
{
_counter++;
}
}
static void Main(string[] args)
{
int threadsCount = 100;
Thread[] threads = new Thread[threadsCount];
//Allocate threads
for (int i = 0; i < threadsCount; i++)
threads[i] = new Thread(DoCount);
//Start threads
for (int i = 0; i < threadsCount; i++)
threads[i].Start();
//Wait for threads to finish
for (int i = 0; i < threadsCount; i++)
threads[i].Join();
//Print counter
Console.WriteLine("Counter is: {0}", _counter);
Console.ReadLine();
}
}
}
appreciate your help.