
2 CHAPTER 1 Hello, world of concurrency in C++!
One of the most significant new features in the C++11 Standard was the support of
multithreaded programs. For the first time, the C++ Standard acknowledged the existence of
multithreaded applications in the language and provided components in the library for writing
multithreaded applications. This made it possible to write multithreaded C++ programs without
relying on platform-specific extensions and enabled you to write portable multithreaded code
with guaranteed behavior. It also came at a time when programmers were increasingly looking
to concurrency in general, and multithreaded programming in particular, to improve application
performance. The C++14 and C++17 Standards have built upon this baseline to provide further
support for writing multithreaded programs in C++, as have the Technical Specifications.
There’s a Technical Specification for concurrency extensions, and another for parallelis m,
though the latter has been incorporated into C++17.
This book is about writing programs in C++ using multiple threads for concurrency and the
C++ language features and library facilities that make it possible. I’ll start by explaining what I
mean by concurrency and multithreading and why you would want to use concurrency in your
applications. After a quick detour into why you might not want to use it in your applications,
we’ll go through an overview of the concurrency support in C++, and we’ll round off this chapter
with a simple example of C++ concurrency in action. Readers experienced with developing
multithreaded applications may wish to skip the early sections. In subsequent chapters, we’ll
cover more extensive examples and look at the library facilities in more depth. The book will
finish with an in-depth reference to all the C++ Standard Library facilities for multithreading
and concurrency.
So, what do I mean by concurrency and multithreading?
1.1 What is concurrency?
At the simplest and most basic level, concurrency is about two or more separate activities
happening at the same time. We encounter concurrency as a natural part of life; we can walk
and talk at the same time or perform different actions with each hand, and we each go about our
lives independently of each other—you can watch football while I go swimming, and so on.
1.1.1 Concurrency in computer systems
When we talk about concurrency in terms of computers, we mean a single system performing
multiple independent activities in parallel, rather than sequentially, or one after the other. This
isn’t a new phenomenon. Multitasking operating systems that allow a single desktop computer
to run multiple applications at the same time through task switching have been commonplace
for many years, as have high-end server machines with multiple processors that enable genuine
concurrency. What’s new is the increased prevalence of computers that can genuinely run
multiple tasks in parallel rather than giving the illusion of doing so.
What is concurrency?
Historically, most desktop computers have had one processor, with a single processing unit or core,
and this remains true for many desktop machines today. Such a machine can only perform one task at a
time, but it can switch between tasks many times per second. By doing a bit of one task and then a bit of