Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!

Using C++11 Lambda functions with the C++Builder Parallel Programming Library

With the introduction of the Parallel Programming Library (PPL) and Embarcadero C++Builder support for C++11 lambda functions on the Win32, Win64, iOS and Android platforms, you can now use the two in combination in your applications. Below you will find C++Builder example source code that uses lambda functions with the Parallel For, Tasks and Futures. If you use C++11 features (including lamda functions) in your applications that target Win32, make sure you turn off (set to False) the “Use classic Borland compiler” in the Projects | Options | C++ Compiler dialog.

Parallel For

The Parallel Programming Library (PPL) includes a Parallel for loop method. The TParallel.For accepts anonymous methods in Delphi whereas in C++ you create an Iterator event function or C++11 lambda and pass that as part of the TParallel::For loop call. In this example, I show you the two ways to use an iterator function in the Parallel::For method: using an event style iterator and using a C++11 lambda. I’ve commented out the use of the iterator function in favor of using the C++11 lambda function. Note: the variable Tot is defined in the public section of the form class.


Parallel Task

The Parallel Programming Library (PPL) provides a TTask class to run one task or multiple tasks in parallel. A Task is a unit of work you need to get done. The PPL does the association between the task and the thread that performs the task so you can run several tasks in parallel without having to create your own custom threads and managing them. This example creates two parallel tasks that use C++11 lambda functions. Inside of the button event handler, the Parallel Task WaitForAny and WaitForAll methods are used.


Parallel Future

This sample shows you how to use C++11 lambda functions with the Parallel Future feature in the Parallel Programming Library. Parallel Future uses TTask::Future to launch a function that returns a specific type. TTask::Future<T>, where the type parameter, T, represents the return type. Using a future allows you to calculate something or run some query and at the same time you can do other tasks, getting the value when you want via future’s value. If the value is not completed yet, the Parallel Programming Library blocks the current code waiting for this value to be calculated. This allows you to prioritize code blocks to run in the order you want, but still ensure you get the value at the point you need it.


Links to Additional Resources


Learn about lambda function C++, the parts of the expressions, and how this function can help you drastically improve how you write code.

Also, learn how to utilize constexpr variables and constexpr functions in this tutorial.

Source Code on Code Central

You can download the source code examples (tested using C++Builder 10 Seattle) for all three projects using the CodeCentral link – “Using C++11 Lambda functions with the C++Builder PPL” – http://cc.embarcadero.com/item/30508.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.