Example #1
0
        internal void Work()
        {
            if (threadStart != null)
            {
                threadStart();
            }
            threadStart = null;

            while (true)
            {
                //When the owning ParallelLooper is told to start a loop, it will notify the worker via this signal.
                getToWork.WaitOne();
                if (manager.currentLoopBody == null)
                {
                    //Woops, looks like it's time for me to die.
                    manager.OnWorkerFinish();
                    return;
                }

                while (manager.jobIndex <= manager.maxJobIndex)
                {
                    //Claim a piece of job.
                    int jobIndex = Interlocked.Increment(ref manager.jobIndex);
                    //The job interval.
                    int endIndex   = jobIndex * iterationsPerSteal;
                    int beginIndex = endIndex - iterationsPerSteal;

                    //Do the job piece.  Make sure you don't do more than exists in the list itself.
                    for (int i = beginIndex; i < endIndex && i < finalIndex; i++)
                    {
                        manager.currentLoopBody(i);
                    }
                } //this is not 'thread safe' but the result of the unsafety is a quick fail in the worst case.

                manager.OnWorkerFinish();
            }
        }