示例#1
0
    /**
     * Test timed wait.
     */
    private static bool TestTimedWait()
    {
        Thread[] tthrs = new Thread[THREADS];
        ManualResetEventSlimOptimized mrevs = new ManualResetEventSlimOptimized(false);                 // our implementation

//		ManualResetEventSlim mrevs = new ManualResetEventSlim(false);		// BCL implementation

        Console.WriteLine("-->Test timed wait");
        for (int i = 0; i < THREADS; i++)
        {
            int tid = i;
            tthrs[i] = new Thread(() => {
                Random rnd = new Random(tid);

                Console.WriteLine($"{tid}, started...");
                bool timedOut = false;
                try {
                    timedOut = !mrevs.Wait(rnd.Next(MIN_TIMEOUT, MAX_TIMEOUT));
                } catch (ThreadInterruptedException) {
                    Console.WriteLine($"-->{tid}, was interrupted while waiting!");
                }
                try {
                    Console.WriteLine($"{tid}, timed out = {timedOut}");
                    Console.WriteLine($"{tid}, exiting...");
                } catch (ThreadInterruptedException) {}
            });
            tthrs[i].Start();
        }

        //
        // Sleep ...
        //

        Thread.Sleep(MAX_TIMEOUT + DEVIATION_TIME);                     // all waiters must time out
        //Thread.Sleep(MIN_TIMEOUT - DEVIATION_TIME);		// none waiter must times out
        //Thread.Sleep((MIN_TIMEOUT + MAX_TIMEOUT) / 2);	// some waiters time out
        bool success = true;

        for (int i = 0; i < THREADS; i++)
        {
            if (tthrs[i].IsAlive)
            {
                success = false;
                Console.WriteLine($"++#{i} is still alive, so it will be interrupted");
                tthrs[i].Interrupt();
            }
        }

        //
        // Wait until all test threads have been exited.
        //

        for (int i = 0; i < THREADS; i++)
        {
            tthrs[i].Join();
        }

        return(success);
    }
示例#2
0
    /**
     * Test Set followed immediately by Reset
     */
    private static bool TestSetFollowedByReset()
    {
        Thread[] tthrs = new Thread[THREADS];
        ManualResetEventSlimOptimized mrevs = new ManualResetEventSlimOptimized(false);         // our implementation

        //ManualResetEventSlim mrevs = new ManualResetEventSlim(false);		// BCL implementation - fails!

        Console.WriteLine("-->Test set followed by reset");
        for (int i = 0; i < THREADS; i++)
        {
            int tid = i;
            tthrs[i] = new Thread(() => {
                Console.WriteLine($"--{tid}, started...");
                try {
                    mrevs.Wait();
                } catch (ThreadInterruptedException) {
                    Console.WriteLine($"-->{tid} was interrupted while waiting!");
                }
                try {
                    Console.WriteLine($"{tid}, exiting...");
                } catch (ThreadInterruptedException) {}
            });
            tthrs[i].Start();
        }

        //
        // Sleep for a while before set the manual-reset event.
        //

        Thread.Sleep(SETUP_TIME);
        mrevs.Set();
        //Thread.Sleep(20);
        mrevs.Reset();
        Thread.Sleep(EXIT_TIME + 500);
        bool success = true;

        for (int i = 0; i < THREADS; i++)
        {
            if (tthrs[i].IsAlive)
            {
                success = false;
                Console.WriteLine($"++#{i} is still alive so it will be interrupted");
                tthrs[i].Interrupt();
            }
        }

        //
        // Wait until all test threads have been exited.
        //

        for (int i = 0; i < THREADS; i++)
        {
            tthrs[i].Join();
        }
        return(success);
    }