Example #1
0
    public static void For(int from, int to, DelegateFor delFor)
    {
        DelegateProcess process = delegate(int chunkStart, int chunkEnd) {
            for (int i = chunkStart; i < chunkEnd; ++i)
            {
                delFor(i);
            }
        };

        int cores  = Environment.ProcessorCount;
        int chunks = (to - from) / cores;

        IAsyncResult[] asyncResults = new IAsyncResult[cores];

        int end = 0;

        for (int i = 0; i < cores; ++i)
        {
            int start = i * chunks;
            end = Math.Min(start + chunks, to);
            asyncResults [i] = process.BeginInvoke(start, end, null, null);
        }

        for (int i = end; i < to; ++i)
        {
            delFor(i);
        }

        for (int i = 0; i < cores; ++i)
        {
            process.EndInvoke(asyncResults [i]);
        }
    }
Example #2
0
        internal void ProcessWithAllId(DelegateProcess delegateProcess)
        {
            var rowsSet = _session.Execute("select id, url from price_crl.html", 100);

            foreach (var VARIABLE in rowsSet)
            {
                long   id  = Convert.ToInt64(VARIABLE.GetValue(typeof(long), "id"));
                string url = Convert.ToString(VARIABLE.GetValue(typeof(string), "url"));
                delegateProcess(new Tuple <long, string>(id, url));
            }
        }
Example #3
0
        /// <summary>
        /// Parallel for loop. Invokes given action, passing arguments
        /// fromInclusive - toExclusive on multiple threads.
        /// Returns when loop finished.
        /// </summary>
        public static void For(int fromInclusive, int toExclusive, ForDelegate forDelegate)
        {
            // chunkSize = 1 makes items to be processed in order.
            // Bigger chunk size should reduce lock waiting time and thus
            // increase paralelism.
            int chunkSize = 4;

            // number of process() threads
            int threadCount = Environment.ProcessorCount;
            int index       = fromInclusive - chunkSize;
            // locker object shared by all the process() delegates
            object locker = new object();

            // processing function
            // takes next chunk and processes it using action
            DelegateProcess process = delegate()
            {
                while (true)
                {
                    int chunkStart = 0;
                    lock (locker)
                    {
                        // take next chunk
                        index     += chunkSize;
                        chunkStart = index;
                    }
                    // process the chunk
                    // (another thread is processing another chunk
                    //  so the real order of items will be out-of-order)
                    for (int i = chunkStart; i < chunkStart + chunkSize; i++)
                    {
                        if (i >= toExclusive)
                        {
                            return;
                        }
                        forDelegate(i);
                    }
                }
            };

            // launch process() threads
            IAsyncResult[] asyncResults = new IAsyncResult[threadCount];
            for (int i = 0; i < threadCount; ++i)
            {
                asyncResults[i] = process.BeginInvoke(null, null);
            }
            // wait for all threads to complete
            for (int i = 0; i < threadCount; ++i)
            {
                process.EndInvoke(asyncResults[i]);
            }
        }
Example #4
0
        /// <summary>
        /// Parallel for loop. Invokes given action, passing arguments
        /// fromInclusive - toExclusive on multiple threads.
        /// Returns when loop finished.
        /// </summary>
        /// <param name="chunkSize">
        /// chunkSize = 1 makes items to be processed in order.
        /// Bigger chunk size should reduce lock waiting time and thus
        /// increase paralelism.
        /// </param>
        /// <param name="threadCount">number of process() threads</param>
        public static void For(int fromInclusive, int toExclusive, int chunkSize, int threadCount, ForDelegate forDelegate)
        {
            int index = fromInclusive - chunkSize;
            // locker object shared by all the process() delegates
            object locker = new object();

            // processing function
            // takes next chunk and processes it using action
            DelegateProcess process = delegate()
            {
                while (true)
                {
                    int chunkStart = 0;
                    lock (locker)
                    {
                        // take next chunk
                        index     += chunkSize;
                        chunkStart = index;
                    }
                    // process the chunk
                    // (another thread is processing another chunk
                    //  so the real order of items will be out-of-order)
                    for (int i = chunkStart; i < chunkStart + chunkSize; i++)
                    {
                        if (i >= toExclusive)
                        {
                            return;
                        }
                        forDelegate(i);
                    }
                }
            };

            // launch process() threads
            IAsyncResult[] asyncResults = new IAsyncResult[threadCount];
            for (int i = 0; i < threadCount; ++i)
            {
                asyncResults[i] = process.BeginInvoke(null, null);
            }
            // wait for all threads to complete
            for (int i = 0; i < threadCount; ++i)
            {
                process.EndInvoke(asyncResults[i]);
            }
        }
Example #5
0
        public static Process CreateLaserEnemyProcessChain(ProcessManager processManager, Engine engine, Entity entity, float waitTime, bool loop)
        {
            Process chain       = new WaitProcess(waitTime);
            Process loopProcess = null;

            if (loop)
            {
                loopProcess = new DelegateProcess(() =>
                {
                    processManager.Attach(CreateLaserEnemyProcessChain(processManager, engine, entity, CVars.Get <float>("laser_enemy_successive_wait_period"), true));
                });
            }
            chain.SetNext(new LaserEnemyFreezeRotation(engine, entity))
            .SetNext(new LaserWarmUpProcess(engine, entity))
            .SetNext(new WaitProcess(CVars.Get <float>("laser_enemy_warm_up_duration")))
            .SetNext(new LaserShootProcess(engine, entity))
            .SetNext(new LaserEnemyUnfreezeRotation(engine, entity))
            .SetNext(loopProcess);
            return(chain);
        }