public void BasicTest_StartWaiters()
        {
            _count = 0;
            const int NumThreads = 10;
            CountDownLatch latch = new CountDownLatch(1);
            EventWaitHandle[] handles = new EventWaitHandle[NumThreads];

            for (int i=0; i<NumThreads; i++)
            {
                handles[i] = new EventWaitHandle(false, EventResetMode.ManualReset);

                Thread thr = new Thread(delegate(object x) { 
                    latch.Await();
                    Interlocked.Increment(ref _count);
                    ((EventWaitHandle)x).Set();
                });
                thr.Name = String.Format("Thread {0}",i);
                thr.Start(handles[i]);
            }
            EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.ManualReset);
            Thread thr1 = new Thread(delegate(object x)
            {
                Thread.Sleep(100);
                EventWaitHandle.WaitAll(handles);
                ((EventWaitHandle)x).Set();
            });
            thr1.SetApartmentState(ApartmentState.MTA);
            thr1.Start(handle);
            
            Thread.Sleep(100);
            Expect(_count,EqualTo(0));
            latch.CountDown();
            handle.WaitOne();
            Expect(_count,EqualTo(NumThreads));
        }
        public void AwaitThatTimesOutReturnsFalse()
        {
            const int NumThreads = 10;
            CountDownLatch latch = new CountDownLatch(NumThreads);

            bool result = latch.Await(50);
            Expect(result, False);
        }
        public void AwaitThatDoesNotTimeOutReturnsTrue()
        {
            const int LatchCount = 10;
            CountDownLatch latch = new CountDownLatch(LatchCount);

            Thread thr = new Thread(delegate()
            {
                Thread.Sleep(100);
                for ( int i=0; i<LatchCount; i++ )
                    latch.CountDown();
            });
            thr.Start();

            bool result = latch.Await(50000);
            Expect(result);
        }
        public void BasicTest_Countdown()
        {
            const int NumThreads = 10;
            CountDownLatch latch = new CountDownLatch(NumThreads);

            for (int i = 0; i < NumThreads; i++)
            {
                Thread thr = new Thread(delegate()
                {
                    Thread.Sleep(100);
                    latch.CountDown();
                });
                thr.Name = String.Format("Thread {0}", i);
                thr.Start();
            }

            latch.Await();
            Expect(latch.Count, EqualTo(0));
        }
Esempio n. 5
0
 /// <summary>
 /// Construct an info.
 /// </summary>
 /// <param name="status">Current status.</param>
 /// <param name="startPoint">Start point.</param>
 public Info(int status, long startPoint)
 {
     _status     = new AtomicInteger(status);
     _startPoint = startPoint;
     _latch      = new CountDownLatch(1);
 }
 /// <summary>
 /// Construct an info.
 /// </summary>
 /// <param name="status">Current status.</param>
 /// <param name="startPoint">Start point.</param>
 public Info(int status, long startPoint)
 {
     _status = new AtomicInteger(status);
     _startPoint = startPoint;
     _latch = new CountDownLatch(1);
 }