Implementing parallel map and fold with tasks
Tasks are a higher-level alternative to threads for performing concurrent computations. std::async()
enables us to execute functions asynchronously, without the need to handle lower-level threading details. In this recipe, we will take the same task of implementing a parallel version of the map
and fold
functions, as in the previous recipe, but we will use tasks and see how it compares with the thread version.
Getting ready
The solution presented in this recipe is similar in many aspects to the one that uses threads in the previous recipe, Implementing parallel map and fold with threads. Make sure you read that one before continuing with the current recipe.
How to do it...
To implement a parallel version of the map
function, do the following:
- Define a function template that takes a begin and end iterator to a range, and a function to apply to all the elements:
template <typename Iter...