/// <summary> /// Rudece /// </summary> public static IEnumerable <TResult> R <TResult, TPartialResult>(this IEnumerable <TPartialResult> pr , Func <TPartialResult, TResult> r, ParallelOptions po = default(ParallelOptions), Func <TPartialResult, bool> filter = default(Func <TPartialResult, bool>)) { if (po == default(ParallelOptions)) { po = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount } } ; var result = new ConcurrentBag <TResult>(); Parallel.ForEach(SingleItemPartitioner.Create(pr) , po , () => new ConcurrentBag <TResult>() , (source, loopState, index, taskFactor) => { if (filter != default(Func <TPartialResult, bool>) && filter(source)) { taskFactor.Add(Task.Factory.StartNew(() => r(source)).Result); } else { taskFactor.Add(Task.Factory.StartNew(() => r(source)).Result); } return(taskFactor); } , e => { e.AsParallel().ForAll(v => result.Add(v)); }); return(result); }
/// <summary> /// 조건이 참을 반환하는 동안에는 반복해서 body 함수를 병렬로 호출합니다. /// </summary> /// <param name="options">병렬 처리 옵션</param> /// <param name="condition">While문의 평가할 조건</param> /// <param name="body">실행할 메소드</param> public static void While(Func <bool> condition, Action body, ParallelOptions options = null) { condition.ShouldNotBeNull("condition"); body.ShouldNotBeNull("body"); options = options ?? DefaultParallelOptions; if (IsDebugEnabled) { log.Debug("조건 함수가 True를 반환할 때까지 body 함수를 병렬로 호출합니다."); } Parallel.ForEach(SingleItemPartitioner.Create(IterateUntilFalse(condition)), options, ignored => body()); }
/// <summary>Repeatedly executes an operation in parallel while the specified condition evaluates to true.</summary> /// <param name="parallelOptions">A ParallelOptions instance that configures the behavior of this operation.</param> /// <param name="condition">The condition to evaluate.</param> /// <param name="body">The loop body.</param> public static void ParallelWhile( ParallelOptions parallelOptions, Func <bool> condition, Action body) { if (parallelOptions == null) { throw new ArgumentNullException(nameof(parallelOptions)); } if (condition == null) { throw new ArgumentNullException(nameof(condition)); } if (body == null) { throw new ArgumentNullException(nameof(body)); } Parallel.ForEach(SingleItemPartitioner.Create(IterateUntilFalse(condition)), parallelOptions, ignored => body()); }
/// <summary>Repeatedly executes an operation in parallel while the specified condition evaluates to true.</summary> /// <param name="parallelOptions">A ParallelOptions instance that configures the behavior of this operation.</param> /// <param name="condition">The condition to evaluate.</param> /// <param name="body">The loop body.</param> public static void ParallelWhile( ParallelOptions parallelOptions, Func <bool> condition, Action body) { if (parallelOptions == null) { throw new ArgumentNullException("parallelOptions"); } if (condition == null) { throw new ArgumentNullException("condition"); } if (body == null) { throw new ArgumentNullException("body"); } System.Threading.Tasks.Parallel.ForEach(SingleItemPartitioner.Create(IterateUntilFalse(condition)), parallelOptions, ignored => body()); }