public static void ReleaseRefTest(SafeRefCountedHolder <object> holder) { Thread[] threads = new Thread[nThreads + 1]; for (int i = 0; i < nThreads + 1; i++) { int threadId = i; threads[i] = new Thread(() => { holder.ReleaseRef(); Console.WriteLine("Thread " + threadId + " released ref current count " + holder.refCount); }); threads[i].Start(); } for (int i = 0; i < nThreads + 1; i++) { threads[i].Join(); } Console.WriteLine("final count " + holder.refCount); try { holder.AddRef(); } catch (InvalidOperationException) { Console.WriteLine("Invalid operation after dispose"); } }
public static void AddRefTest(SafeRefCountedHolder <object> holder) { Thread[] threads = new Thread[nThreads]; for (int i = 0; i < nThreads; i++) { int threadId = i; threads[i] = new Thread(() => { holder.AddRef(); Console.WriteLine("Thread " + threadId + " added ref current count " + holder.refCount); }); threads[i].Start(); } for (int i = 0; i < nThreads; i++) { threads[i].Join(); } Console.WriteLine("final count " + holder.refCount); }