Parallel Loops
From The Oxygene Language Wiki
This is a Language topic
Feel free to add your notes to this topic below.
A parallel loop is a "for" loop that is spread over multiple cores and threads through the use of the Microsoft PFX framework (currently in beta). Delphi Prism has a syntax that allows you to turn a regular "for" loop into one that runs in parallel. This syntax only works when the PFX framework is referenced. Although the elements of the loop are spread over multiple threads/cpus, the loop doesn't finish until all individual elements are done. When an exception occurs in one of the iterations the loop is canceled, finishing the currently running elements and it will wrap the exception(s) in a new exception that will be thrown in the context of the original thread.
The use of Exit is not allowed in parallel loops. Break will stop the loop from running further elements but the already running ones will continue. Continue will end the current iteration while proceeding to the next in line. Both for each and for syntaxes can be run in parallel although downto is not allowed (and makes no sense given that the execution order is not guaranteed).
for parallel i: Integer := 0 to 10 do ... // works for parallel i: Integer := 1 to 49 step 7 do ... //works for parallel i: Integer := 10 downto 0 do ...// doesn't work
The syntax for 'for each' is:
for each parallel elem in SomeCollection do ...
Here it is required, that SomeCollection implements the generic IEnumerable<T>.
Beware that there is a bug in June 2008 CTP version of the PFX library, that can leads to a high CPU usage after a parallel loop. For more information, see Using Parallel.For keeps CPU busy when it is done
See Also
Area: Oxygene Language
Compiler version: Oxygene 5
Language Glossary — Keywords — Types — FAQ — How To