Example #1
0
        /// <summary>
        /// 多线程并行建图
        /// </summary>
        /// <param name="threadCount">并行线程数量</param>
        private unsafe void buildGraph(int threadCount)
        {
            node bootNode = NodePool[boot];

            Monitor.Enter(nodeLock);
            try
            {
                list <int> reader = getBuildGraphReader(ref bootNode);
                if (reader.Count != 0)
                {
                    int            taskCount = threadCount - 1;
                    graphBuilder[] builders  = new graphBuilder[threadCount];
                    for (int builderIndex = 0; builderIndex != builders.Length; builders[builderIndex++].Set(ref bootNode))
                    {
                        ;
                    }
                    using (fastCSharp.threading.task task = new fastCSharp.threading.task(taskCount))
                    {
                        do
                        {
                            int[] readerArray = reader.UnsafeArray;
                            int   count = reader.Count / threadCount, index = 0;
                            for (int builderIndex = 0; builderIndex != taskCount; ++builderIndex)
                            {
                                builders[builderIndex].Set(readerArray, index, index += count);
                                task.Add(builders[builderIndex].Build);
                            }
                            builders[taskCount].Set(readerArray, index, reader.Count);
                            builders[taskCount].Build();
                            task.WaitFree();
                            reader.Empty();
                            for (int builderIndex = 0; builderIndex != builders.Length; ++builderIndex)
                            {
                                list <int> writer = builders[builderIndex].Writer;
                                if (writer.Count != 0)
                                {
                                    reader.Add(writer.UnsafeArray, 0, writer.Count);
                                }
                            }
                        }while (reader.Count != 0);
                    }
                }
            }
            finally { Monitor.Exit(nodeLock); }
        }
Example #2
0
        /// <summary>
        /// 单线程建图
        /// </summary>
        private unsafe void buildGraph()
        {
            node         bootNode = NodePool[boot];
            graphBuilder builder  = new graphBuilder();

            builder.Set(ref bootNode);
            Monitor.Enter(nodeLock);
            try
            {
                list <int> reader = getBuildGraphReader(ref bootNode);
                while (reader.Count != 0)
                {
                    builder.Set(reader.UnsafeArray, 0, reader.Count);
                    builder.Build();
                    list <int> values = reader;
                    reader         = builder.Writer;
                    builder.Writer = values;
                }
            }
            finally { Monitor.Exit(nodeLock); }
        }