public static void GlobalThread(int id, bool waitAll) { int fail = 0; int count = 0; try { do { if (waitAll ? StWaitable.WaitAll(evts, new StCancelArgs(shutdown)) : StWaitable.WaitAny(evts, new StCancelArgs(id, shutdown)) != StParkStatus.Timeout) { count++; } else { fail++; } } while (!shutdown.IsSet); } catch (StThreadAlertedException) { } VConsole.WriteLine("+++ {0} (Wait{1}) exiting: {2}/{3}", Thread.CurrentThread.Name, waitAll ? "All" : "Any", count, fail); done.Signal(); }
private static void Run(int id) { int fail = 0; var sem0 = new StSemaphore(0); var sem1 = new StSemaphore(0); var sem2 = new Semaphore(0, int.MaxValue); var sem3 = new Semaphore(0, int.MaxValue); var stsems = new[] { sem0, sem1 }; var whsems = new[] { sem2, sem3 }; VConsole.WriteLine("+++ w #{0} started...", id); do { ThreadPool.QueueUserWorkItem(delegate { sem0.Release(1); sem1.Release(1); Thread.Sleep(0); sem2.Release(1); sem3.Release(1); if (sem1.Wait(1, new StCancelArgs(0))) { Thread.Sleep(0); sem1.Release(1); } }); try { do { if (StWaitable.WaitAll(stsems, whsems, new StCancelArgs(id))) { break; } fail++; } while (true); } catch (StThreadAlertedException) { break; } if ((++counts[id] % 1000) == 0) { VConsole.Write("-{0}", id); } } while (shutdown == 0); VConsole.WriteLine("+++ w #{0} exiting: [{1}/{2}]", id, counts[id], fail); done.Signal(); }
private void Run() { int count = 0; int failed = 0; Console.WriteLine("+++ ctrl #{0} starts...\n", id); Random rnd = new Random(Environment.TickCount * (id + 1)); StTimer[] timers = new StTimer[TIMERS]; for (int i = 0; i < TIMERS; i++) { timers[i] = new StTimer(true); } do { // // Start the timers. // int maxTime = 0; for (int i = 0; i < TIMERS; i++) { int delay = 100 + rnd.Next(500); timers[i].Set(delay, 0, TimerCallback, timers[i]); if (delay > maxTime) { maxTime = delay; } } int start = Environment.TickCount; try { int timeout = 350 + rnd.Next(500); if (StWaitable.WaitAll(timers, new StCancelArgs(timeout, shutdown))) { int elapsed = Environment.TickCount - start; Console.WriteLine("+++ ctrl #{0}, synchronized with its timers[{1}/{2}]\n", id, maxTime, elapsed); count++; Thread.Sleep(100); if (!shutdown.IsSet) { continue; } } else { int elapsed = Environment.TickCount - start; Console.WriteLine("--- ctrl #{0}, timed out({1}) expired[{2}/{3}]\n", id, timeout, maxTime, elapsed); } } catch (StThreadAlertedException) { Console.WriteLine("--- ctrl #{0}, CANCELLED\n", id); } // // Cancel the timers. // for (int i = 0; i < TIMERS; i++) { timers[i].Cancel(); } failed++; } while (!shutdown.IsSet); for (int i = 0; i < TIMERS; i++) { timers[i].Cancel(); } Console.WriteLine("+++ ctrl #{0} exiting after [{1}/{1}] synchs...\n", id, count, failed); done.Signal(); }