Functional programming to the rescue!
As we’ve seen, one of the problems with parallel and concurrent tasks is the shared access to resources. Functional programming, in its pure form, solves this out of the box through immutability. Since everything is immutable by default, and since any change to a value is made by pointing to a changed value instead of modifying the initial one, threads are never at risk of modifying data used by other threads. We discussed how to achieve this when we discussed the different paradigms available in C++.
But there’s more: functional programming offers us parallelizable algorithms out of the box. The C++ standardization committee recognized this when introducing functional algorithms along with an execution policy that allows you to run operations on collections in parallel.
Let’s look at a simple example: we want to compute the sum of squares of the values in a collection. The functional version of this type of algorithm...