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 a good news to have a standard way to manage the concurrency in C++, indeed before the introduced features in the new standards, many libraries and ways existed to manage the concurrency in the C++ projects.

In this post we will discover the advantages of reading the “Concurrency with modern C++” book written by Rainer Grimm.

Pragmatic approach

In the computer science world there are many academic books mostly destined to students and there are some other books mainly destined to the developers who needs advises of how to resolve real problems when developing a product. This book follow a pragmatic approach and from the beginning, it references the examples package and how to compile them. The goal is to practice each solution provided in the book.

The Whole picture from the beginning:

In general to have an idea about a book content we can take a look at the index and read the introduction. However in many cases it’s not really sufficient to have a clear idea about the goal of the book.

What’s interesting in this book is that we have a “Quick Overview” section which contains 8 pages and explains clearly the problematic, the solutions and what the new standards provides as features.

Memory Model clearly explained

From the quick overview 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 figure out easily how to deal with memory in case of concurrency. However what’s more interesting is the pictures used to schematize the problems and the solutions.

In this section we can also discover all the new standards features to deal with the memory in case of the concurrency 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 add resolving a bug or adding a code become a headache. Sometimes resolving a bug can introduce many other ones.

So it’s recommended to master some multithreading basics before using them. Fortunately C++ has 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 the threads.

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

And the same as the memory model section many code samples and pictures are provided to figure out easily how to deal with multithreading.

Case studies to  practice the theory.

After providing the theory on the memory model and the multithreading interface, The author apply the theory in practice and provide you a few performance numbers.

The case studies are very easy to understand like the “calculating the sum of a vector” one. However the author explains deeply 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 for any developer who want to master the C++ concurrency .

Challenges and best practices at the end

After discovering the memory model, the multithreading and some case studies to master them, the author provides some interesting challenges, here’s from the author the motivation behind 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 also the author provides some best practices to deal with the concurrency as he explained:

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 worked on many C++ projects to resolve a real problems. Which explains the pragmatic approach of the book. We recommend this book to each developer who want to master the concurrency especially by using the new C++ standards.