Delphi 200X、XE中如何用并行实现循环的计算
2017-04-14 23:36
375 查看
interface uses Classes, SysUtils; type TParallelProc = reference to procedure(i: Integer; ThreadID: Integer); TParallel = class(TThread) private FProc: TParallelProc; FThreadID: Integer; //current thread ID protected procedure Execute; override; function GetNextValue: Integer; public constructor Create; destructor Destroy; override; property Proc: TParallelProc read FProc write FProc; class var CurrPos: Integer; //current loop index MaxPos: Integer; //max loops index cs: TCriticalSection; ThCount: Integer; //thread counter - how much threads have finished execution end; {** ParallelFor Loop - all iterations will be performed in chosen threads @param nMin - Loop min value (first iteration) @param nMax - Loop max value (last iteration) @param nThreads - how much threads to use @param aProc - anonymous procedure which will be performed in loop thread } procedure ParallelFor(nMin, nMax, nThreads: Integer; aProc: TParallelProc); overload; {** ParallelFor Loop - all iterations will be performed in max cpu cores @param nMin - Loop min value (first iteration) @param nMax - Loop max value (last iteration) @param aProc - anonymous procedure which will be performed in loop thread } procedure ParallelFor(nMin, nMax: Integer; aProc: TParallelProc); overload; implementation uses {$IFDEF MSWINDOWS} Windows, {$ENDIF} SyncObjs; procedure ParallelFor(nMin, nMax, nThreads: Integer; aProc: TParallelProc); var threads: array of TParallel; I: Integer; begin if nMin > nMax then Exit; // initialize TParallel class data TParallel.CurrPos := nMin; TParallel.MaxPos := nMax; TParallel.cs := TCriticalSection.Create; TParallel.ThCount := 0; // create the threads SetLength (threads, nThreads); for I := 0 to nThreads - 1 do begin threads[I] := TParallel.Create; // suspended threads[I].FThreadID := I; threads[I].Proc := aProc; threads[I].Start; end; for I := 0 to nThreads - 1 do begin threads[I].WaitFor; end; for I := 0 to nThreads - 1 do begin threads[I].Free; end; TParallel.cs.Free; end; procedure ParallelFor(nMin, nMax: Integer; aProc: TParallelProc); begin ParallelFor(nMin, nMax, CPUCount, aProc); end; { TParallel } constructor TParallel.Create; begin inherited Create(True); // suspended InterlockedIncrement(ThCount); FreeOnTerminate := False; FThreadID := 0; end; destructor TParallel.Destroy; begin InterlockedDecrement(ThCount); inherited; end; procedure TParallel.Execute; var nCurrent: Integer; begin nCurrent := GetNextValue; while nCurrent <= MaxPos do begin Proc(nCurrent, FThreadID); nCurrent := GetNextValue; end; end; function TParallel.GetNextValue: Integer; begin cs.Acquire; try Result := CurrPos; Inc(CurrPos); finally cs.Release; end; end; http://blog.csdn.net/zang141588761/article/details/51505360[/code]
相关文章推荐
- Delphi 200X、XE中如何用并行实现循环的计算
- Delphi 200X、XE中如何用并行实现循环的计算
- 一、Fourinone如何实现多线程并行计算
- 【并行计算-CUDA开发】有关CUDA当中global memory如何实现合并访问跟内存对齐相关的问题
- Fourinone如何实现并行计算和数据库引擎
- 请教高手:如何利用Delphi实现多线程圆周率的计算
- 如何实现PHP异步调用或者说并行计算
- 如何实现PHP异步调用或者说并行计算
- Zabbix数据结构及并行计算实现 推荐
- JDK 7 中的 Fork/Join 模式轻松实现多核时代的并行计算
- 在VC中如何实现对存储过程的循环调用?
- 求救!胜负比率计算问题?如何用SQL语句实现?(存储过程也可以)
- Delphi的DLL里如何实现定时器功能?
- linux命令parallel实现多进程并行计算
- C循环如何实现详解
- 【Python】【并行计算】Python的GIL是什么鬼,多线程性能究竟如何
- Flink流计算编程--如何实现基于KEY/VALUE的List State
- 原生js如何实现柱状图以及原生js柱状图结合ajax循环动态数据_JavaScript_柱状图(原生JavaScript做的柱状图)(03)_ajax获取数据
- (ZZ)如何实现游戏主循环(Game Loop)的详细解析
- 如何用DELPHI实现把WORD、EXCEL和图片等存储到数据库中