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 the classic for
? The difference is that the anonymous method passed to the for
method is executed on different threads concurrently; this is the reason it's a parallel for, as the for block is executed in parallel. This means...