SpawnThreadIfNeeded() private method

Checks if our situation calls for a new thread. If so, spawns one.
private SpawnThreadIfNeeded ( ) : bool
return bool
コード例 #1
0
        public void CreatesNewThreadsWhenMoreThanAskedForWorkItems( )
        {
            ThreadManager tm = new ThreadManager(TimeSpan.MinValue, TimeSpan.MaxValue,
                2, TimeSpan.MaxValue);
            int i = -2;
            int cur_threads = tm.NumThreads;
            for ( int j = 0; j < 3; j++ )
                tm.EnqueueTask(new ActionTask(( _ ) => i++, false));
            bool result = tm.SpawnThreadIfNeeded();
            Assert.IsTrue(result, "Did not report that it spawned more threads.");
            Assert.IsTrue(cur_threads < tm.NumThreads, "Did not actually spawn more threads.");

            ThreadManager tm2 = new ThreadManager(TimeSpan.MinValue, TimeSpan.MaxValue,
                6, TimeSpan.MaxValue);
            cur_threads = tm2.NumThreads;
            for ( i = 0; i < 3; i++ )
                tm2.EnqueueTask(new ActionTask(( _ ) => i++, false));
            result = tm2.SpawnThreadIfNeeded();
            Assert.IsFalse(result, "Reported that it spawned more threads before reaching the max queue length.");
            Assert.IsTrue(cur_threads == tm2.NumThreads, "Spawned a new thread before reaching the max queue length.");
            for ( i = 0; i < 3; i++ )
                tm2.EnqueueTask(new ActionTask(( _ ) => i++, false));
            result = tm2.SpawnThreadIfNeeded();
            Assert.IsTrue(result, "Did not report that it spawned more threads.");
            Assert.IsTrue(cur_threads < tm2.NumThreads, "Did not actually spawn more threads.");
        }
コード例 #2
0
        public void MinTimeActuallyStopsCreationOfThreads( )
        {
            ThreadManager tm = new ThreadManager(TimeSpan.FromMilliseconds(100), TimeSpan.MinValue,
                uint.MaxValue, TimeSpan.MaxValue);
            int i = 0;
            int cur_threads = tm.NumThreads;
            tm.EnqueueTask(new ActionTask(( _ ) => i++, false));
            bool result = tm.SpawnThreadIfNeeded();
            Assert.IsFalse(result, "Reported that it needed new threads before reaching the timeout.");
            Assert.AreEqual(cur_threads, tm.NumThreads, "Spawned another thread before reaching the timeout");
            System.Threading.Thread.Sleep(100);
            result = tm.SpawnThreadIfNeeded();
            Assert.IsTrue(result, "Did not report that it spawned more threads.");
            Assert.IsTrue(cur_threads < tm.NumThreads, "Did not actually spawn more threads.");
            // Second thread...
            cur_threads = tm.NumThreads;
            result = tm.SpawnThreadIfNeeded();
            Assert.IsFalse(result, "Reported that it needed new threads before reaching the timeout.");
            Assert.AreEqual(cur_threads, tm.NumThreads, "Spawned another thread before reaching the timeout");
            System.Threading.Thread.Sleep(100);
            result = tm.SpawnThreadIfNeeded();
            Assert.IsTrue(result, "Did not report that it spawned more threads.");
            Assert.IsTrue(cur_threads < tm.NumThreads, "Did not actually spawn more threads.");

            ThreadManager tm2 = new ThreadManager(TimeSpan.FromMilliseconds(300), TimeSpan.MinValue,
                uint.MaxValue, TimeSpan.MaxValue);
            cur_threads = tm2.NumThreads;
            tm2.EnqueueTask(new ActionTask(( _ ) => i++, false));
            result = tm2.SpawnThreadIfNeeded();
            Assert.IsFalse(result, "Reported that it needed new threads before reaching the timeout.");
            Assert.AreEqual(cur_threads, tm2.NumThreads, "Spawned another thread before reaching the timeout");
            System.Threading.Thread.Sleep(100);
            result = tm2.SpawnThreadIfNeeded();
            Assert.IsFalse(result, "Reported that it needed new threads before reaching the timeout.");
            Assert.AreEqual(cur_threads, tm2.NumThreads, "Spawned another thread before reaching the timeout");
            System.Threading.Thread.Sleep(200);
            result = tm2.SpawnThreadIfNeeded();
            Assert.IsTrue(result, "Did not report that it spawned more threads.");
            Assert.IsTrue(cur_threads < tm2.NumThreads, "Did not actually spawn more threads.");
        }