Book Image

C++ High Performance - Second Edition

By : Björn Andrist, Viktor Sehr
5 (2)
Book Image

C++ High Performance - Second Edition

5 (2)
By: Björn Andrist, Viktor Sehr

Overview of this book

C++ High Performance, Second Edition guides you through optimizing the performance of your C++ apps. This allows them to run faster and consume fewer resources on the device they're running on without compromising the readability of your codebase. The book begins by introducing the C++ language and some of its modern concepts in brief. Once you are familiar with the fundamentals, you will be ready to measure, identify, and eradicate bottlenecks in your C++ codebase. By following this process, you will gradually improve your style of writing code. The book then explores data structure optimization, memory management, and how it can be used efficiently concerning CPU caches. After laying the foundation, the book trains you to leverage algorithms, ranges, and containers from the standard library to achieve faster execution, write readable code, and use customized iterators. It provides hands-on examples of C++ metaprogramming, coroutines, reflection to reduce boilerplate code, proxy objects to perform optimizations under the hood, concurrent programming, and lock-free data structures. The book concludes with an overview of parallel algorithms. By the end of this book, you will have the ability to use every tool as needed to boost the efficiency of your C++ projects.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

Awaitable types revisited

We already talked a bit about awaitable types in the previous chapter. But now we need to get a little bit more specific about what co_await does and what an awaitable type is. The keyword co_await is a unary operator, meaning that it takes a single argument. The argument we pass to co_await needs to fulfill some requirements that we will explore in this section.

When we say co_await in our code, we express that we are waiting for something that may or may not be ready for us. If it's not ready, co_await suspends the currently executing coroutine and returns control back to its caller. When the asynchronous task has completed, it should transfer the control back to the coroutine originally waiting for the task to finish. From here on, I will typically refer to the awaiting function as the continuation.

Now consider the following expression:

co_await X{};

For this code to compile, X needs to be an awaitable type. So...