예제 #1
0
        private void AdvanceSingleThreaded()
        {
            var task = new AdvanceTask(0, CurrentGeneration.Count, this);

            task.Start();
            CurrentGeneration = new Generation <T>(task.NextGeneration, CurrentGeneration.Age + 1);
        }
예제 #2
0
        private void AdvanceMultiThreaded()
        {
            int threadCount = Environment.ProcessorCount - 1;
            int taskSize    = CurrentGeneration.Count / threadCount;

            var tasks = new AdvanceTask[threadCount];

            for (int i = 0; i < threadCount - 1; i++)
            {
                tasks[i] = new AdvanceTask(i * taskSize, taskSize, this);
            }

            int lastTaskIndex = (tasks.Length - 1) * taskSize;

            tasks[tasks.Length - 1] =
                new AdvanceTask(lastTaskIndex, CurrentGeneration.Count - lastTaskIndex, this);

            var threads = new Thread[threadCount];

            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(tasks[i].Start);
            }

            foreach (var thread in threads)
            {
                thread.Start();
            }

            for (int i = 0; i < tasks.Length; i++)
            {
                var task = tasks[i];
                while (!task.Completed)
                {
                    threads[i].Join();
                }
            }

            var nextGenerationModules = new List <Module <T> >();

            foreach (var task in tasks)
            {
                nextGenerationModules.AddRange(task.NextGeneration);
            }

            CurrentGeneration = new Generation <T>(nextGenerationModules, CurrentGeneration.Age + 1);
        }