// // Test timed wait. // private static bool TestTimedWait() { Thread[] tthrs = new Thread[THREADS]; ManualResetEventSlim_ mrevs = new ManualResetEventSlim_(false); for (int i = 0; i < THREADS; i++) { int tid = i; tthrs[i] = new Thread(() => { string stid = string.Format("#{0}", tid); Random rnd = new Random(tid); Console.WriteLine("{0}, started...", stid); bool timedOut = false; try { timedOut = !mrevs.Wait(rnd.Next(MIN_TIMEOUT, MAX_TIMEOUT)); } catch (ThreadInterruptedException) { Console.WriteLine("-->{0}, was interrupted while waiting!", stid); } try { Console.WriteLine("{0}, timed out = {1}", stid, timedOut); Console.WriteLine("{0}, exiting...", stid); } catch (ThreadInterruptedException) {} }); tthrs[i].Start(); } // // Sleep ... // Thread.Sleep(MAX_TIMEOUT + DEVIATION_TIME); //Thread.Sleep(MIN_TIMEOUT - DEVIATION_TIME); 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); }
/* * Test Set followed immediately by Reset */ private static bool TestSetFollowedByReset() { Thread[] tthrs = new Thread[THREADS]; //ManualResetEventSlim mrevs = new ManualResetEventSlim(false); ManualResetEventSlim_ mrevs = new ManualResetEventSlim_(false); for (int i = 0; i < THREADS; i++) { int tid = i; tthrs[i] = new Thread(() => { string stid = string.Format("#{0}", tid); Console.WriteLine("--{0}, started...", stid); try { mrevs.Wait(); } catch (ThreadInterruptedException) { Console.WriteLine("-->{0}, was interrupted while waiting!", stid); } try { Console.WriteLine("{0}, exiting...", stid); } catch (ThreadInterruptedException) {} }); tthrs[i].Start(); } // // Sleep for a while before set the manual-reset event. // Thread.Sleep(SETUP_TIME); mrevs.Set(); 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); }
/* * Test normal wait */ private static bool TestSetFollowedByReset() { Thread[] tthrs = new Thread[THREADS]; ManualResetEventSlim_ mrevs = new ManualResetEventSlim_(false); for (int i = 0; i < THREADS; i++) { int tid = i; tthrs[i] = new Thread(() => { string stid = string.Format("#{0}", tid); Console.WriteLine("--{0}, started...", stid); try { mrevs.Wait(); } catch (ThreadInterruptedException) { Console.WriteLine("-->{0}, was interrupted while waiting!", stid); } try { Console.WriteLine("{0}, exiting...", stid); } catch (ThreadInterruptedException) {} }); tthrs[i].Start(); } // // Sleep for a while before set the manual-reset event. // Thread.Sleep(MAX_TIMEOUT); mrevs.Set(); //Thread.Sleep(1); 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; }
// // Test timed wait. // private static bool TestTimedWait() { Thread[] tthrs = new Thread[THREADS]; ManualResetEventSlim_ mrevs = new ManualResetEventSlim_(false); for (int i = 0; i < THREADS; i++) { int tid = i; tthrs[i] = new Thread(() => { string stid = string.Format("#{0}", tid); Random rnd = new Random(tid); Console.WriteLine("{0}, started...", stid); bool timedOut = false; try { timedOut = !mrevs.Wait(rnd.Next(MIN_TIMEOUT, MAX_TIMEOUT)); } catch (ThreadInterruptedException) { Console.WriteLine("-->{0}, was interrupted while waiting!", stid); } try { Console.WriteLine("{0}, timed out = {1}]", stid, timedOut); Console.WriteLine("{0}, exiting...]", stid); } catch (ThreadInterruptedException) {} }); tthrs[i].Start(); } // // Sleep ... // Thread.Sleep(MAX_TIMEOUT + DEVIATION_TIME); //Thread.Sleep(MIN_TIMEOUT - DEVIATION_TIME); 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; }