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 := ; // create the threads
SetLength (threads, nThreads);
for I := to nThreads - do
begin
threads[I] := TParallel.Create; // suspended
threads[I].FThreadID := I;
threads[I].Proc := aProc;
threads[I].Start;
end; for I := to nThreads - do
begin
threads[I].WaitFor;
end; for I := to nThreads - 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 := ;
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;

Delphi 200X、XE中如何用并行实现循环的计算的更多相关文章

  1. Delphi 2010 XE 中使用 JSON 之 SuperObject68-6

    JSON之SuperObject(1):一直盼着Delphi能够直接支持"正则:Delphi2009刚来的时候,有了JSON,但:Delphi2010带了两个相关单元:DBXJS:我想不等了 ...

  2. [转]:Delphi XE中泛型数组的使用范例

    Delphi XE中泛型数组的使用范例,下面的范例简单的使用了泛型字符串数组,如用 TArray 代替 array of Word, 还可以使用 TArray 类提供的算法(就是少了点). uses ...

  3. Delphi 7~XE系列升级安装Indy10.6

    由于低版本Indy无法满足网络技术的日益更新,如SSL/TLS请求.RawHeaders与Cookie管理等问题处理. 我本身一直在用Delphi 2007,因为D2009开始底层的编码已不同,旧项目 ...

  4. Delphi XE7并行编程: 并行For循环

    从Delphi XE7开始,引入了全新的并行编程库用于简化并行编程,它位于System.Threading单元中. 下面是一个判断素数的简单例子:function IsPrime (N: Intege ...

  5. Delphi 提示在Delphi的IDE中,按Ctrl+Shift+G键可以为一个接口生成一个新的GUID。

    对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...

  6. ng1中 如何用双向绑定 实现单向绑定的初始时不显示双括号效果?

    ng1中 如何用双向绑定 实现单向绑定(ng-bind就可以不显示{{}})的初始时不显示双括号效果? AngularJS 实例 页面加载时防止应用闪烁: <div ng-app="& ...

  7. linux中如何用root去修改其他用户的密码

    linux中如何用root去修改其他用户的密码 昨天linux实验课,我有很多自己想摸索的东西.今天周五,本是下午一二节是编译的实验,可强烈的欲望让我今早就来实验室了,摸索吧,碰到了这个问题....  ...

  8. JavaSE中线程与并行API框架学习笔记——线程为什么会不安全?

    前言:休整一个多月之后,终于开始投简历了.这段时间休息了一阵子,又病了几天,真正用来复习准备的时间其实并不多.说实话,心里不是非常有底气. 这可能是学生时代遗留的思维惯性--总想着做好万全准备才去做事 ...

  9. JDK1.8中如何用ScriptEngine动态执行JS

    JDK1.8中如何用ScriptEngine动态执行JS jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持.这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言.而JD ...

随机推荐

  1. BZOJ 3437: 小P的牧场 斜率优化DP

    3437: 小P的牧场 Description 背景 小P是个特么喜欢玩MC的孩纸... 描述 小P在MC里有n个牧场,自西向东呈一字形排列(自西向东用1…n编号),于是他就烦恼了:为了控制这n个牧场 ...

  2. chche缓存

    打开一张图片,先从缓存中找,如果没有,再去sccard中找,如果还没有,就去网络下载.下载好了以后,先保存到sdcard中,再保存到缓存中 public class ImageAsyncTask ex ...

  3. 使用github上的开源框架SlidingMenu环境的搭建,以及getSupportActionBar方法不能找到的问题

    http://blog.csdn.net/lovexieyuan520/article/details/9814273 使用github上的开源框架SlidingMenu环境的搭建,以及getSupp ...

  4. exec命令

    exec 命令实例 find . -name "*.cc" -exec grep -P -n -H --color=auto "[^\w]main[^\w]" ...

  5. 图文详解YUV420, yuv格式2

    YUV格式有两大类:planar和packed. 对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V. 对于packed的YUV格式,每个像素点的Y ...

  6. Knowledgeroot安装与使用入门

    采用 PHP 开发的知识库系统,基于树状结构对内容进行组织.使用 FCKEditor 进行内容编辑. 效果http://demo.knowledgeroot.org/index.php?id=2230 ...

  7. Javascript实现Linq查询方式

    Linq是.net平台一个重要的技术,全称Language Integrated Query.通过构建快速查询语句,可快速从数据库或集合中筛选数据集.以查询数据库相同的方式操作内存数据. 在ECMAS ...

  8. 递推DP URAL 1586 Threeprime Numbers

    题目传送门 /* 题意:n位数字,任意连续的三位数字组成的数字是素数,这样的n位数有多少个 最优子结构:考虑3位数的数字,可以枚举出来,第4位是和第3位,第2位组成的数字判断是否是素数 所以,dp[i ...

  9. c++ string 结束符‘\000’

    昨天输出string类型时总是出错,发现输出到文件的一行里多了^@,输出到console却看不到,debug发现,string类型中多了\000,这主要由于我想要用\0截掉字符串最后一位,所以把字符串 ...

  10. c/c++ 笔试面试题

    #include <iostream> using namespace std; class A { public: void sayHi(){ cout<<"hel ...