Ejemplo n.º 1
0
        public void ForExecution <TState, T>(int from, int to, int step,
                                             Func <T, TState> state, Action <T, ThreadLocalState <TState> > function,
                                             InvokeAction <T, ThreadLocalState <TState> > aggregation, T value)
        {
            TState initialState = state(value);
            List <ForPartAggregation <TState, T> > forParts = new List <ForPartAggregation <TState, T> >();

            chunk = (to - from) / processorCount;
            int start = from;
            int end   = chunk;

            for (int i = 1; i <= processorCount; i++)
            {
                int x = i - 1;
                waitHandles[x] = new ManualResetEvent(false);
                ForPartAggregation <TState, T> forPartAggregation = new ForPartAggregation <TState, T>(start, end, step);
                forPartAggregation.ExecutionPart = function;
                forPartAggregation.Statevalue    = new ThreadLocalState <TState>(initialState);
                forPartAggregation.Value1        = value;
                forParts.Add(forPartAggregation);
                ConstantForAggregationSynchronisationContainer <TState, T> forSynchronisationContainer =
                    new ConstantForAggregationSynchronisationContainer <TState, T>((ManualResetEvent)waitHandles[x], forPartAggregation);
                ThreadPool.QueueUserWorkItem(
                    delegate(object localstate)
                {
                    ConstantForAggregationSynchronisationContainer <TState, T> localforSynchronisationContainer = (ConstantForAggregationSynchronisationContainer <TState, T>)localstate;
                    ForPartAggregation <TState, T> localforPart = localforSynchronisationContainer.ForPart_;
                    try
                    {
                        for (int ii = localforPart.From; ii <= localforPart.To; ii = ii + localforPart.Step)
                        {
                            localforPart.ExecutionPart.Invoke(ref ii, localforPart.Value1, localforPart.Statevalue);
                        }
                    }
                    finally
                    {
                        localforSynchronisationContainer.ManualResetEvent_.Set();
                    }
                }, forSynchronisationContainer);
                start = end + 1;
                end   = end + chunk;
            }
            WaitHandle.WaitAll(waitHandles);
            foreach (ForPartAggregation <TState, T> localforPart in forParts)
            {
                aggregation(localforPart.Value1, localforPart.Statevalue);
            }
        }
Ejemplo n.º 2
0
 public ConstantForAggregationSynchronisationContainer(ManualResetEvent manualResetEvent, ForPartAggregation <T> forPart)
     : base(manualResetEvent)
 {
     this.forPart_ = forPart;
 }