コード例 #1
0
        public void TestDispose()
        {
            MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U);
            Thread             slaveA      = new Thread(testBarrier.SlaveWaitForFirstOpen);
            Thread             slaveB      = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveA.Start();
            LosgapSystem.InvokeOnMaster(() => testBarrier.Dispose());
            slaveB.Start();

            slaveA.Join();
            slaveB.Join();

            testBarrier = new MasterSlaveBarrier(2U);
            slaveA      = new Thread(() => {
                testBarrier.SlaveWaitForFirstOpen();
                testBarrier.SlaveWaitForReset();
            });
            slaveB = new Thread(() => {
                testBarrier.SlaveWaitForFirstOpen();
                testBarrier.SlaveWaitForReset();
            });

            slaveA.Start();
            slaveB.Start();

            LosgapSystem.InvokeOnMaster(() => {
                testBarrier.MasterOpenBarrier();
                testBarrier.MasterWaitForClose();
                testBarrier.Dispose();
            });

            slaveA.Join();
            slaveB.Join();
        }
コード例 #2
0
        public void TestOpenAndClose()
        {
            MasterSlaveBarrier testBarrier = new MasterSlaveBarrier(2U);
            Thread             slaveA      = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveA.Start();
            Thread slaveB = new Thread(testBarrier.SlaveWaitForFirstOpen);

            slaveB.Start();
            Assert.IsFalse(slaveA.Join(TimeSpan.FromSeconds(0.5d)));
            Assert.IsFalse(slaveB.Join(TimeSpan.FromSeconds(0.5d)));
            LosgapSystem.InvokeOnMaster(testBarrier.MasterOpenBarrier);
            slaveA.Join();
            slaveB.Join();

            slaveA = new Thread(testBarrier.SlaveWaitForReset);
            slaveA.Start();
            slaveB = new Thread(testBarrier.SlaveWaitForReset);

            bool   barrierClosed     = false;
            object barrierClosePulse = new object();

            LosgapSystem.InvokeOnMasterAsync(() => {
                testBarrier.MasterWaitForClose();
                testBarrier.MasterOpenBarrier();
                lock (barrierClosePulse) {
                    barrierClosed = true;
                    Monitor.Pulse(barrierClosePulse);
                }
            });

            Thread.Sleep(TimeSpan.FromSeconds(0.5d));
            Assert.IsFalse(barrierClosed);

            Monitor.Enter(barrierClosePulse);
            slaveB.Start();
            slaveA.Join();
            slaveB.Join();
            Monitor.Wait(barrierClosePulse);
            Monitor.Exit(barrierClosePulse);
            Assert.IsTrue(barrierClosed);

            testBarrier.Dispose();
        }
コード例 #3
0
        internal ParallelizationProvider()
        {
            NumThreads = (uint)(Environment.ProcessorCount + LosgapSystem.ThreadOversubscriptionFactor);
            if (NumThreads > LosgapSystem.MaxThreadCount)
            {
                NumThreads = LosgapSystem.MaxThreadCount;
            }

            numSlavesStillRunning = NumThreads - 1U;
            WorkBarrier           = new MasterSlaveBarrier(numSlavesStillRunning);

#if DEBUG
            slaveThreads = new Thread[NumThreads - 1];
            for (int i = 0; i < NumThreads - 1; ++i)
            {
                slaveThreads[i] = new Thread(StartSlave)
                {
                    Priority       = LosgapSystem.SlaveThreadPriority,
                    Name           = "LosgapSlave_" + i,
                    IsBackground   = true,
                    CurrentCulture = new CultureInfo("en-US")
                };
                slaveThreads[i].Start();
            }
#else
            for (int i = 0; i < NumThreads - 1; ++i)
            {
                new Thread(StartSlave)
                {
                    Priority       = LosgapSystem.SlaveThreadPriority,
                    Name           = "LosgapSlave_" + i,
                    IsBackground   = true,
                    CurrentCulture = new CultureInfo("en-US")
                }.Start();
            }
#endif
        }