예제 #1
0
        private static bool TestCompletion()
        {
            const int MIN_TIMEOUT = 0;
            const int MAX_TIMEOUT = 10;
            const int RUN_TIME = 10000;
            const int THREADS = 50;
            const int units = 20;
            Thread[] tthrs = new Thread[THREADS];
            int[] privateCounters = new int[THREADS];
            int sharedCounter = 0;
            Completion a = new Completion(units , false);

            for (int i = 0; i < THREADS; i++)
            {
                int tid = i;
                tthrs[i] = new Thread(() =>
                {
                    Random rnd = new Random(tid);
                    // Wait until start event is set
                    int endTime = Environment.TickCount + RUN_TIME;

                    do
                    {
                        a.WaitForCompletion(tid + 1);
                        sharedCounter++;
                        a.Complete();

                        if ((++privateCounters[tid] % 100) == 0)
                        {
                            Console.Write("[#{0}: {1}]", tid, privateCounters[tid]);
                        }
                        else
                        {
                            Thread.Sleep(rnd.Next(MIN_TIMEOUT, MAX_TIMEOUT));
                        }
                    } while (Environment.TickCount < endTime);
                });
                tthrs[i].Start();
            }

            // Wait until all threads have been terminated.
            for (int i = 0; i < THREADS; i++)
                tthrs[i].Join();

            // Compute results

            Console.WriteLine("\nPrivate counters:\n");
            int sum = 0;
            for (int i = 0; i < THREADS; i++)
            {
                sum += privateCounters[i];
                if ((i % 5) == 0)
                    Console.WriteLine();
                else
                    Console.Write(' ');
                Console.Write("[#{0}: {1,4}]", i, privateCounters[i]);
            }
            //All threads get a unit?
            return sum == sharedCounter;
        }
예제 #2
0
        public static void Test1() //test Complete
        {
            int        nThreads   = 4;
            Completion completion = new Completion();

            Thread[] threads = new Thread[nThreads];

            for (int i = 0; i < nThreads; i++)
            {
                int threadId = i;
                threads[i] = new Thread(() =>
                {
                    try
                    {
                        Console.WriteLine("Test1:Thread " + threadId + " started");
                        if (completion.WaitForCompletion(Timeout.Infinite))
                        {
                            Console.WriteLine("Test1:Thread " + threadId + " WaitForCompletion exited successfully.");
                        }
                        else
                        {
                            Console.WriteLine("Test1:Thread " + threadId + " WaitForCompletion timed out.");
                        }
                    }
                    catch (ThreadInterruptedException)
                    {
                        Console.WriteLine("Test1:Thread " + threadId + " interrupted.");
                    }
                });
                threads[i].Start();
            }

            Thread releaseThread = new Thread(() => {
                for (int i = 0; i < nThreads; i++)
                {
                    while (threads[i].ThreadState == ThreadState.Running)
                    {
                        Thread.Sleep(1000);
                    }

                    while (threads[i].ThreadState == ThreadState.WaitSleepJoin)
                    {
                        Thread.Sleep(1000);
                        completion.Complete();
                        Console.WriteLine("Complete called.");
                    }
                }
            });

            releaseThread.Start();

            for (int i = 0; i < nThreads; i++)
            {
                if (threads[i] != null)
                {
                    threads[i].Join();
                }
            }

            releaseThread.Join();
        }