HikariThreading.ThreadManager.WaitForThreadSpawns C# (CSharp) Method

WaitForThreadSpawns() private method

Waits for all Threads to spawn. This is to make automated testing easier.
private WaitForThreadSpawns ( ) : void
return void
        internal void WaitForThreadSpawns( )
        {
            while ( true )
            {
                lock ( threadLock )
                    if ( numThreads == threads.Count )
                        break;

                System.Threading.Thread.Sleep(1);
            }
        }

Usage Example

Example #1
0
        public void LetsOthersUseNappingThreadsCorrectly( )
        {
            int i = 0;
            object _lock = new object();
            ThreadManager tman = new ThreadManager(1, 1);
            // Using this to test interaction very specifically.
            tman.WaitForThreadSpawns();
            ActionTask task = new ActionTask(( _ ) =>
            {
                _.IsNapping = true;
            }, false);
            task.Extend(( _ ) => i = 100);
            tman.EnqueueTask(task);
            for ( int j = 0; j < 10; j++ )
                tman.EnqueueTask(new ActionTask(( _ ) =>
                {
                    lock ( _lock )
                        i++;
                }, false));

            tman.UnsafeUpdate();
            for ( int j = 0; j < 100; j++ )
                if ( task.IsNapping )
                    break;
                else
                    System.Threading.Thread.Sleep(1);

            for ( int j = 0; j < 10; j++ )
            {
                tman.UnsafeUpdate();
                System.Threading.Thread.Sleep(1);
                lock ( _lock ) Assert.AreEqual(j + 1, i, "Ran some number other than 1 of the Tasks at once on update #" + j);
            }

            task.IsNapping = false;
            tman.UnsafeUpdate();
            System.Threading.Thread.Sleep(1);
            Assert.AreEqual(100, i, "Did not run awakened Task.");

            tman.Dispose();
        }