BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News A Task Parallel Library for Object Pascal and C++

A Task Parallel Library for Object Pascal and C++

A major feature of RAD Studio XE7 is its Parallel Programming Library. XE7 brings task-based parallelism to a variety of platforms including Windows, OS X, iOS, and Android. Unlike Mono, this tool-chain offers fully native applications on all target platforms.

At the core of the XE7 Parallel Programming Library are tasks and futures. These are available as both interfaces (ITask, IFuture) and classes (TTask, TFuture). These are equivalent to .NET’s Task and Task<T> classes.

When working with discrete operations, tasks and futures are generally used on their own. If you want to parallelize a set of operations, then wrappers come into play. For example, you can abstract the task logic needed to parallelize a for-loop using code such as this:

TParallel.For(1, Max, procedure (I: Integer)
    begin
        if IsPrime (I) then
        TInterlocked.Increment (TotalPrimes);
    end);

For those of you unfamiliar with Object Pascal, the ‘procedure’ keyword creates an anonymous function. This is similar in behavior to VB’s ‘Function’ keyword or C#’s arrow syntax.

Note the use of the TInterlocked.Increment class. This is a platform-agnostic version of the Windows Interlocked.Increment. Without it, the TotalPrimes count may be incorrect due to race conditions. So while this library simplifies parallel coding, the developer is still responsible for ensuring it is done correctly.

A major flaw in the Parallel Programming Library isn’t the code itself, but rather the documentation. Of the 18 classes and types, only seven of them even have a one-line description. While sometimes acceptable for a free/open source library, this is quite unusual for a modern commercial offering.

Rate this Article

Adoption
Style

BT