[Solved] Duplicate math result in Bakery Algorithm (C# code)


You must create a different random number for each thread (more details)

so try this code in your main method:

for (int i = 0; i < 10; i++)
{
     int temp = i;
     threadArray[i] = new Thread(() => simThread(temp));
     Console.WriteLine("[He Thong] PID " + i.ToString() + " duoc khoi tao");
     threadArray[i].Start();
     Thread.Sleep(20);
}

and the following code in you threads:

Random rand = new Random((int) DateTime.Now.Ticks & 0x0000FFFF);

now you can ensure you produce different random number for each thread.

13

solved Duplicate math result in Bakery Algorithm (C# code)