Delphi 200X、XE中如何用并行实现循环的计算
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Delphi 200X、XE中如何用并行实现循环的计算,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2810字,纯文字阅读大概需要5分钟。
内容图文
![Delphi 200X、XE中如何用并行实现循环的计算](/upload/InfoBanner/zyjiaocheng/1099/97d1174245744ecaa6415811f7002b0a.jpg)
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
原文:http://www.cnblogs.com/findumars/p/6711427.html
内容总结
以上是互联网集市为您收集整理的Delphi 200X、XE中如何用并行实现循环的计算全部内容,希望文章能够帮你解决Delphi 200X、XE中如何用并行实现循环的计算所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。