Example #1
0
        public ParallelInvoker(Game game, ParallelInvokerTarget <T> function, bool multiThreaded)
        {
            Function = function;

            if (multiThreaded)
            {
                // Try to pick a good number of threads based on the current CPU count.
                // Too many and we'll get bogged down in scheduling overhead. Too few and we won't benefit from parallelism.
                ThreadCount = Math.Max(2, Math.Min(8, Environment.ProcessorCount));
            }
            else
            {
                ThreadCount = 1;
            }

            UserData = new T();

            Delegates = new InvokerFunction[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                int j = i;
                Delegates[i] =
                    () => InvokeInternal(j);
            }
        }
        public ParallelInvoker(Game game, ParallelInvokerTarget <T> function, bool multiThreaded)
        {
            Function = function;

            if (multiThreaded)
            {
#if XBOX
                // ThreadPoolComponent always uses 3 threads on XBox 360.
                ThreadCount = 3;
#else
                // Try to pick a good number of threads based on the current CPU count.
                // Too many and we'll get bogged down in scheduling overhead. Too few and we won't benefit from parallelism.
                ThreadCount = Math.Max(2, Math.Min(8, Environment.ProcessorCount));
#endif
            }
            else
            {
                ThreadCount = 1;
            }

            UserData = new T();

            Delegates = new InvokerFunction[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                int j = i;
                Delegates[i] =
                    () => InvokeInternal(j);
            }

#if XBOX
            Signals = new AutoResetEvent[ThreadCount];
            for (int i = 0; i < ThreadCount; i++)
            {
                Signals[i] = new AutoResetEvent(false);
            }

            foreach (var component in game.Components)
            {
                ThreadPool = component as ThreadPoolComponent;
                if (ThreadPool != null)
                {
                    break;
                }
            }

            if (ThreadPool == null)
            {
                throw new InvalidOperationException("You must have a ThreadPoolComponent to use a ParallelInvoker");
            }
#endif
        }