Blog 5 min read

Master C++ Concurrency with 'Concurrency with Modern C++

Share this article
Master C++ Concurrency with 'Concurrency with Modern C++

C++11 and beyond introduced a new thread library. This library includes utilities for starting and managing threads. It also contains utilities for synchronization like mutexes and other locks, atomic variables and other utilities.

It’s good news to have a standard way to manage concurrency in C++; indeed, before the features introduced in the new standards, many libraries and approaches existed to manage concurrency in C++ projects.

In this post we will look at the advantages of reading the book "Concurrency with Modern C++" written by Rainer Grimm.

Pragmatic approach

In the computer science world there are many academic books mostly intended for students, and other books mainly intended for developers who need advice on how to solve real problems when developing a product. This book follows a pragmatic approach, and from the beginning it references the examples package and explains how to compile them. The goal is to practice each solution provided in the book.

The whole picture from the beginning

In general, to get an idea of a book’s content we can take a look at the table of contents and read the introduction. However, in many cases that’s not really enough to have a clear idea of the book’s goals.

What’s interesting about this book is that it has an 8-page “Quick Overview” section that clearly explains the problem, the solutions, and the features the new standards provide.

Memory model clearly explained

From the quick overview on, the memory model constraints are clearly explained.

The foundation of multithreading is a well-defined memory model. This memory model has to deal with the following aspects: • Atomic operations: operations that can be performed without interruption. • Partial ordering of operations: sequence of operations that must not be reordered. • Visible effects of operations: guarantees when operations on shared variables are visible in other threads.

And in the section dedicated to the memory model, a deep analysis of the memory constraints is provided, with many code samples to easily understand how to deal with memory in concurrent scenarios. But what’s even more interesting is the pictures used to illustrate the problems and the solutions.

In this section we also discover all the new standards’ features for dealing with memory in concurrent programs.

Multithreading demystified

Too many programmers writing multithreaded programs learn to create a bunch of threads and get them mostly working, but then the threads go completely out of control and the programmer doesn’t know what to do. After that, fixing a bug or adding code becomes a headache. Sometimes fixing one bug can introduce many others.

So it’s recommended to master some multithreading basics before using them. Fortunately, C++ has had a multithreading interface since C++11, and this interface has all the basic building blocks for creating multithreaded programs. It’s better to have a standard way to deal with threads.

What’s interesting in the Multithreading section of the book is that things are explained step by step; it’s very useful even for a beginner who doesn’t know exactly what threads are. And I also recommend this section to C++ experts. Personally, even though I worked with threads for many years, I discovered many interesting facts and new features about them.

And as in the memory model section, many code samples and pictures are provided to easily understand how to deal with multithreading.

Case studies to practice the theory

After presenting the theory on the memory model and the multithreading interface, the author applies it in practice and provides you with a few performance numbers.

The case studies are very easy to understand, like the “calculating the sum of a vector” one. Moreover, the author explains in depth the different implementation possibilities using new standard features.

After reading and practicing the first case study, you will have a concrete idea of many concurrency issues and how to deal with them. I recommend this case study to any developer who wants to master C++ concurrency.

Challenges and best practices at the end

After covering the memory model, multithreading and some case studies to master them, the author provides some interesting challenges; here’s his motivation for adding them:

Programming concurrently is inherently complicated. This holds in particular true if you use C++11 and C++14 features. I have not even talked about the memory model yet. My hope is that if I dedicate a whole chapter to the challenges of concurrent programming, you may become more aware of the pitfalls.

And the author also provides some best practices to deal with concurrency, as he explains:

This chapter provides you with a simple set of rules for writing well-defined and fast, concurrent programs in modern C++. Multithreading, and in particular parallelism and concurrency, is quite a new topic in C++; therefore, more and more best practices will be discovered in the coming years. Consider the rules in this chapter not as a complete list; but rather as a necessary starting point that will evolve over time.

Conclusion

When reading the book, it’s clear that the author is a programmer who has worked on many C++ projects to solve real problems, which explains the pragmatic approach of the book. We recommend this book to every developer who wants to master concurrency, especially with the new C++ standards.

Share this article