德尔福 基础

本文介绍Embarcadero并行编程库中的TParallel.For函数,该函数可以将循环操作并行化执行。文章通过Delphi和C++代码示例详细解释了如何将普通循环转换为并行循环,并提供了使用闭包和functor处理循环外部变量的方法。

并行

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_TParallel.For_from_the_Parallel_Programming_Library

Using TParallel.For from the Parallel Programming Library

 

Go Up to Using the Parallel Programming Library


The Parallel Programming Library (PPL) includes a loop function, TParallel.For, that allows to run its loops in parallel.

Within parallel loops, you must transform your operations into thread-safe operations. You can use members of the System.SyncObjs unit, such as TInterlocked methods.

Converting a For Loop into a TParallel.For Loop

Delphi:

For LoopTParallel.For Loop
for I := 1 to 10 do begin // … end; 
TParallel.For(1, 10, procedure(I: Integer) begin // … end); 

C++:

If you are using a Clang-enhanced C++ compiler, you can use lambda expressions:

For LoopTParallel.For Loop
for (int i = 1; i <= 10; i++) { // … } 
TParallel::For(0, 10, TProc1<int>([](int I){ // … })); 

You can alternatively use a functor, which works with both previous-generation compilers and Clang-enhanced compilers:

For LoopTParallel.For Loop
for (int i = 1; i <= 10; i++) { // (Loop logic) } 
class TMyFunctor : public TCppInterfacedObject<TProc__1<int> > { public: void __fastcall Invoke(int I) { // (Loop logic) } }; // … TParallel::For(1, 10, System::DelphiInterface<TProc__1<int> >(new TMyFunctor())); 

If the loop needs access to one or more variables from the context outside the callback function, you must use a functor as follows:

  1. Declare variables in the functor class to hold copies or references to the variables that you need.
  2. Declare a constructor of the functor class that accepts the copies or references to those variables as parameters and initializes the corresponding functor variables with their values.
  3. Pass those variables to the contructor when you instantiate the functor.
  4. In your logic, in Invoke(), you can access those functor variables.

For example:

class TMyFunctor : public TCppInterfacedObject<TProc__1<int> > { public: TMyFunctor(int a, TB *b) : a(a), b(b) {} void __fastcall Invoke(int I) { // (Loop logic) } int a; TB *b; }; // … TParallel::For(1, 10, System::DelphiInterface<TProc__1<int> >(new TMyFunctor(a, b)));





分类方法

 TTest = class;
 
 // Declaring metaclass type
 TTestClass = class of TTest; TTest = class public // Class methods class function add(I, J: Integer): Integer; class function GetClsName: string; // Virtual class method class function GetClsNameVirt: string; virtual; // Class getter and setter class function GetCount: Integer; class procedure SetCount(I: Integer); // Virtual class getter and setter class function GetStrProp: string; virtual; class procedure SetStrProp(N: string); virtual; // Class static class function GetStaticCount: Integer; static; class procedure SetStaticCount(I: Integer); static; // Class properties property Count: Integer read GetCount write SetCount; // Non-virtual property StrProp: string read GetStrProp write SetStrProp; // Virtual g/setters end; // Function that takes a class reference function RegisterTestType(Cls: TTestClass): boolean;




基本句法元素(Delphi)


http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Fundamental_Syntactic_Elements_(Delphi)

程序和功能(德尔福)

 

 http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Procedures_and_Functions_(Delphi)

表达式(德尔福)

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Expressions_(Delphi)

编译器属性  volatile

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Compiler_Attributes#Volatile

 

System.TimeSpan.TTimeSpan

 http://docwiki.embarcadero.com/Libraries/Tokyo/en/System.TimeSpan.TTimeSpan

使用并行编程库

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Using_the_Parallel_Programming_Library

简单类型(Delphi)

http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Simple_Types_(Delphi)

 

转载于:https://www.cnblogs.com/marklove/p/9503194.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值