Example

 using System; using System.Threading; using System.Collections; namespace Samples { public class InterlockedSample { private static Hashtable ht = new Hashtable(); public static void Add(object key, object value) { Monitor.Enter(ht); ht.Add(key, value); Thread.Sleep(1000); Monitor.Exit(ht); } public static void Display() { Monitor.Enter(ht); IDictionaryEnumerator ide = ht.GetEnumerator(); while(ide.MoveNext()) Console.WriteLine("Key: {0}, Value: {1}", ide.Key, ide.Value); Monitor.Exit(ht); } public static void StartHere(Object o) { Add(Interlocked.Increment(ref number), o); if(Interlocked.Decrement(ref threads) == 0) threadsDone.Set(); } static int number = 0; static int threads = 5; static AutoResetEvent threadsDone = new AutoResetEvent(false); ...

Get .NET Framework Standard Library Annotated Reference, Volume 1: Base Class Library and Extended Numerics Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.