How to realize parallel loop in Delphi (Delphi 200X, Delphi XE) ? How to do this best way? And is there any universal solution?
With examples, please.
Probably the best solution at the moment is the Parallel For Loop construct in OmniThreadLibrary. You pass it a collection, or a pair of integers representing lower and upper bounds, and an anonymous method representing the loop body, and it uses a thread pool to run the for loop in parallel.
Note that this will only work if the loop body method is capable of standing on its own. If it modifies any external variables or relies on the value of a calculation made earlier in the loop then it can't be parallelized.
An introduction to the OmniThreadLibrary parallel for can be found here. For example, a simple for loop iterating over numbers looks like this:
Parallel.ForEach(1, testSize).Execute(
procedure (const elem: integer)
begin
// do something with 'elem'
end);