static void Main(string[] args) { InterLockedCounter c = new InterLockedCounter(); Thread[] threads = new Thread[5]; for (int i = 0; i < threads.Length; ++i) { threads[i] = new Thread(c.UpdateFields); threads[i].Start(); } for (int i = 0; i < threads.Length; ++i) threads[i].Join(); Console.WriteLine("Filed1: {0} , Field2: {1}\n\n", c.Field1, c.Field2); }
private static void BadAsync() { Console.WriteLine("Singr Interlocded=menthod:"); InterLockedCounter c = new InterLockedCounter(); Thread[] threads = new Thread[5]; for (int i = 0; i < threads.Length; ++i) { threads[i] = new Thread(c.UpdateFields); threads[i].Start(); } for (int i = 0; i < threads.Length; ++i) { threads[i].Join(); } Console.WriteLine("Fiels1: {0}, Field2: {1}\n\n", c.Field1, c.Field2); }