Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
roth_stephan_clean_c20_sustainable_software_development_patt.pdf
Скачиваний:
29
Добавлен:
27.03.2023
Размер:
7.26 Mб
Скачать

Chapter 5 Advanced Concepts of Modern C++

is, reimplementing something (a library or a framework) that is available somewhere else. The reasoning behind this attitude is often the belief that in-house developments must be better in several respects. They are often mistakenly regarded as cheaper, more secure, more flexible, and more controllable than existing and well-established solutions.

In fact, only a few companies succeed in developing a truly equivalent, or even better alternative, to a solution that exists on the market. Often, the enormous effort of such developments does not justify the low benefit. And not infrequently is the self-developed library or framework clearly worse in quality compared to existing and mature solutions that have existed for years.

Over the past decades, many excellent libraries and frameworks have emerged in the C++ environment. These solutions had the chance to mature over a long time, and have been used successfully in tens of thousands of projects. There is no need to reinvent the wheel. Good software craftspeople should know about these libraries. It is not required to know every tiny detail about these libraries and their APIs. It is just good to know, however, that there are tried-and-tested solutions for certain fields of application, which are worth looking at to take into a narrower selection for your software development project.

Take Advantage of <algorithm>

“If you want to improve the code quality in your organization, replace all your coding guidelines with one goal: No raw loops!”

—Sean Parent, principal software architect with Adobe, at CppCon 2013

Fiddling with collections of elements is everyday business in programming. Regardless of whether we are dealing with collections of measurement data, with emails, strings, records from a database, or other elements, software must filter them, sort them, delete them, manipulate them, and more.

In many programs, we can find “raw loops” (e.g., hand-crafted for loops or while loops) for visiting some or all elements in a container, or sequence, in order to do something with them. A simple example is to reverse an order of integers stored in a std::vector this way:

#include <vector>

std::vector<int> integers { 2, 5, 8, 22, 45, 67, 99 };

183

Chapter 5 Advanced Concepts of Modern C++

// ...somewhere in the program: std::size_t leftIndex = 0;

std::size_t rightIndex = integers.size() - 1;

while (leftIndex < rightIndex) { int buffer = integers[rightIndex];

integers[rightIndex] = integers[leftIndex]; integers[leftIndex] = buffer;

++leftIndex; --rightIndex;

}

Basically this code will work. But it has several disadvantages. It is difficult to see immediately what this piece of code is doing (in fact, the first three lines inside the while loop could be substituted with std::swap from the <utility> header). Furthermore, writing code this way is very tedious and error prone. Just imagine that, for any reason, we violate the boundaries of the vector and try to access an element at a position out of range. Unlike member function std::vector::at(), std::vector::operator[] does not raise a std::out_of_range exception then. It will lead to undefined behavior.

The C++ Standard Library provides more than 100 useful algorithms that can be applied to containers or sequences for searching, counting, and manipulating elements. They are collected in the <algorithm> header.

For example, to reverse the order of elements in any kind of Standard Library container, for example, in a std::vector, we can simply use std::reverse:

#include <algorithm>

#include <vector>

std::vector<int> integers = { 2, 5, 8, 22, 45, 67, 99 };

//...somewhere in the program: std::reverse(begin(integers), end(integers));

//The content of 'integers' is now: 99, 67, 45, 22, 8, 5, 2

Unlike our self-written solution before, this code is much more compact, less error prone, and easier to read. Since std::reverse is a function template (like all other algorithms too), it is universally applicable to all Standard Library sequence containers,

184

Chapter 5 Advanced Concepts of Modern C++

associative containers, unordered associative containers, std::string, and primitive arrays (which, by the way, should not be used anymore in a modern C++ program; see the section “Prefer Standard Library Containers over Simple C-Style Arrays” in Chapter 4). See Listing 5-23.

Listing 5-23.  Applying std::reverse to a C-Style Array and a String

#include <algorithm>

#include <string>

// Works, but primitive arrays should not be used in a modern C++ program int integers[] = { 2, 5, 8, 22, 45, 67, 99 }; std::reverse(begin(integers), end(integers));

std::string text { "The big brown fox jumps over the lazy dog!" }; std::reverse(begin(text), end(text));

// Content of 'text' is now: "!god yzal eht revo spmuj xof nworb gib ehT"

The reverse algorithm can be applied, of course, also to sub-ranges of a container or sequence, as shown in Listing 5-24.

Listing 5-24.  Only a Sub-Area of the String Is Reversed

std::string text { "The big brown fox jumps over the lazy dog!" }; std::reverse(begin(text) + 13, end(text) - 9);

// Content of 'text' is now: "The big brown eht revo spmuj xof lazy dog!"

Easier Parallelization of Algorithms Since C++17

“Your free lunch will soon be over.”

—Herb Sutter [Sutter04]

The previous quote, which was addressed to software developers all over the world, is taken from an article published by Herb Sutter, member of the ISO C++ standardization committee, in 2004. It was at a time when the clock rates of processors stopped increasing from year to year. In other words, serial-processing speed has reached a physical limit. Instead, processors were increasingly equipped with more cores. This development in processor architectures leads to a heavy consequence: developers can

185

Chapter 5 Advanced Concepts of Modern C++

no longer take advantage of ever-increasing processor performance by clock rates—the “free lunch” that Herb was talking about—but they will be forced to develop massively multithreaded programs as a way to better utilize modern multi-core processors. As a result, developers and software architects now need to consider parallelization in their software architecture and design.

Before the advent of C++11, the C++ standard supported only single-threaded programming, and you have to use third-party libraries (e.g., Boost.Thread) or compiler extensions (e.g., Open Multi-Processing—OpenMP) to parallelize your programs. Since C++11, the Thread Support Library is available to support multithreaded and parallel programming. This extension of the Standard Library has introduced threads, mutual exclusions, condition variables, and futures.

Parallelizing a section of code requires good problem knowledge and must be considered in the software design accordingly. Otherwise, subtle errors caused by race conditions can occur that could be very difficult to debug. Especially for the algorithms of the Standard Library, which often have to operate on containers that are filled with a huge number of objects, the parallelization should be simplified for developers in order to exploit today’s modern multi-core processors.

Starting with C++17, parts of the Standard Library have been redesigned according to the Technical Specification for C++ Extensions for Parallelism (ISO/IEC TS 19570:2015), also known as the Parallelism TS (TS stands for technical specification). In other words, with C++17 these extensions became part of the mainline ISO C++ standard. Their main goal is to relieve developers a bit from the complex task of fiddling around with those low-level language features from the Thread Support Library, such as std::thread, std::mutex, etc.

In fact that means that about 70 well-known algorithms were overloaded and are now also available in one or more versions accepting an extra template parameter for parallelization called ExecutionPolicy. Some of these algorithms are, for instance, std::for_each, std::transform, std::copy_if, or std::sort. Furthermore, seven new algorithms have been added that can also be parallelized, like std::reduce, std::exclusive_scan, or std::transform_reduce. These new algorithms are particularly useful in functional programming, which is why I discuss them in Chapter 7.

186

Chapter 5 Advanced Concepts of Modern C++

EXECUTION POLICIES [C++17/C++20]

With the appearance of the standard C++17, a majority of algorithm templates from the <algorithm> header have been overloaded and are also available in a parallelizable version. For example, in addition to the existing template for the std::find function, another version has been defined that takes an additional template parameter to specify the execution policy:

//Standard (single-threaded) version: template< class InputIt, class T >

constexpr InputIt find( InputIt first, InputIt last, const T& value );

//Additional version with user-definable execution policy (since C++17): template< class ExecutionPolicy, class ForwardIt, class T >

ForwardIt find(ExecutionPolicy&& policy, ForwardIt first, ForwardIt last, const T& value);

The four standard policy tags that are available in C++20 for the template parameter

ExecutionPolicy are:

•\ std::execution::seq (since C++17). An execution policy type that defines that a parallel algorithm’s execution may be sequentially. Hence, it is more or less the same as you would use the single-threaded standard version of the algorithm template function without an execution policy.

•\ std::execution::par (since C++17.) An execution policy type that defines that a parallel algorithm’s execution may be parallelized. It permits the implementation to execute the algorithm on multiple threads. Important: The parallel algorithms do not automatically protect against critical data races or deadlocks! You are responsible for ensuring that no data race conditions can occur while executing the function.

•\ std::execution::par_unseq (since C++17). An execution policy type that defines that a parallel algorithm’s execution may be vectorized, parallelized, or migrated across threads. Vectorization takes advantage of the SIMD (Single Instruction, Multiple Data) command set of modern CPUs. SIMD means

that a processor can perform the same operation on multiple data points simultaneously.

187