I read following thread unsafe example in page 871 of C# 7.0 in a Nutshell: The Definitive Reference.
public class ThreadUnsafe
{
static int val1=1, val2=1;
public static void Go()
{
if (val2 != 0) Console.WriteLine(val1 / val2);
val2 = 0;
}
}
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(x => ThreadUnsafe.Go());
}
ThreadUnsafe.Go();
Console.WriteLine("Press any key for continuing...");
Console.ReadKey();
}
}
I have written a test program and launch 2 or more threads but cannot get the exception of division by zero.
I have also run this test program on my PC that have 4 core CPU and on 1 CPU VM, I also cannot get this exception of division by zero.
Could you help me that how can I run this sample code in thread unsafe and get the exception of division by zero?