Ejemplo n.º 1
0
        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");
            }
        }
Ejemplo n.º 2
0
        public static void Main()
        {
            SafeRefCountedHolder <object> holder = new SafeRefCountedHolder <object>(new object());

            AddRefTest(holder);
            ReleaseRefTest(holder);
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        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);
        }