Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL

Chapter 8. Designing concurrent code > Designing concurrent code in practice - Pg. 255

Designing concurrent code in practice 255 8.5 Designing concurrent code in practice When designing concurrent code for a particular task, the extent to which you'll need to consider each of the issues described previously will depend on the task. To demonstrate how they apply, we'll look at the implementation of parallel versions of three functions from the C++ Standard Library. This will give you a familiar basis on which to build, while providing a platform for looking at the issues. As a bonus, we'll also have usable imple- mentations of the functions, which could be used to help with parallelizing a larger task. I've primarily selected these implementations to demonstrate particular tech- niques rather than to be state-of-the-art implementations; more advanced implemen- tations that make better use of the available hardware concurrency may be found in the academic literature on parallel algorithms or in specialist multithreading libraries such as Intel's Threading Building Blocks. 4 The simplest parallel algorithm conceptually is a parallel version of std::for_ each , so we'll start with that. 8.5.1 A parallel implementation of std::for_each std::for_each is simple in concept; it calls a user-supplied function on every element in a range in turn. The big difference between a parallel implementation and the sequential std::for_each is the order of the function calls. std::for_each calls