Parallelize using the parallel for

One of the first loops that any programmer starts to know is the for loop. In this recipe, we'll see a particular type of for loop: the parallel one. To be clear, this parallel for loop is not a new language feature but is a sort of it implemented as a static class method.

The parallel for loop is part of the Parallel Programming Library and is implemented by the TParallel class. Here's one of its (overloaded) versions and a utilization example:

//declaration
class method TParallel.&For(ALowInclusive, AHighInclusive: 
Integer; const AIteratorEvent: TProc<Integer>): TLoopResult;

//used as follows
TParallel.&For(1,10,
  procedure(Index: Integer)
  begin 
    //executed 10 times with index 1..10
  end);

What is different about ...

Get Delphi Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.