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.
Table of Contents
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
- C++ option – use ‘classic’ Borland compiler – http://docwiki.embarcadero.com/RADStudio/Seattle/en/C%2B%2B_Compiler#Classic_Compiler
- Using the Parallel Programming Library – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_the_Parallel_Programming_Library
- Using Parallel For – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_TParallel.For_from_the_Parallel_Programming_Library
- Tutorial: Using Parallel For – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_the_For_Loop_from_the_Parallel_Programming_Library
- Using Parallel Tasks – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_TTask_from_the_Parallel_Programming_Library
- Tutorial: Using Parallel Tasks – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_Tasks_from_the_Parallel_Programming_Library
- Using Parallel Futures – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Using_TTask.IFuture_from_the_Parallel_Programming_Library
- Tutorial: Using Parallel Futures – http://docwiki.embarcadero.com/RADStudio/Seattle/en/Tutorial:_Using_Futures_from_the_Parallel_Programming_Library
- C++ Lamda Functions – http://en.cppreference.com/w/cpp/language/lambda
- C++ Lambda Closures – http://www.cprogramming.com/c++11/c++11-lambda-closures.html
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.