Book Review: Real-Time C++

Written by on December 23, 2018 in Book Review, C++ with 0 Comments

Title: Real-Time C++: Efficient Object-Oriented and Template Microcontroller Programming

Author: Christopher Kormanyos

Publisher: Springer-Verlag

Year: 2018 (Third Edition)

It was the title of this book which caught my attention a few weeks ago and I decided to invest in it. I am not in to real-time software development these days, but I always welcome material that enhances my knowledge of C++, and software design in general.  This is the third edition of the book that has been updated to include material on C++17. After reading the book, I realise that the book is more about C++, touching upon microcontroller programming in a few chapters and with very little discussion on real-time programming issues (although many chapters use the words real-time in the title). Sure, there are overlapping concerns between microcontroller programming and real-time programming, but real-time programming is a much more complex subject involving not just resource constraints, but more importantly, time constraints and safety-criticality as well. Sadly, this book does not address the latter.

An embedded system need not be a real-time system. See this and this. Watch this video if you are interested to know the basic concepts of real-time programming.

Chapter 1 starts with a simple C++ class for controlling an LED in a circuit. The author uses the example to introduce some key C++ concepts including class members and namespaces. While explaining the process of initialising const member variables, he writes as follows:

“Constant member variables must be initialized in the constructor initialization list. Non-constant member variables should be initialized in the constructor initialization list. “

The author seems to be playing with words here. Such a piece of text might be appropriate in a literary context, but not in a programmer’s book! Hope you understand what he is getting at.

Chapter 2 discusses the example of programming a simple solderless prototyping board to flash an LED. In the process, the author shows the different steps for compiling C++ code and transferring it to the board.

The 3rd chapter is titled An Easy Jump Start in Real-Time C++. This is just an overview of the common topics in C++. Two sections namely, 3.14 (atomic_load() and atomic_store()) and 3.18 (Using alignof and alignas), are useful in the context of embedded programming, but the other sections are only of general interest. 

The 4th chapter is Object-Oriented Techniques for Microcontrollers. Here the author covers the basics of inheritance and dynamic binding. The chapter includes sections on how to define an abstract class,  non-copyable classes, constant member functions, and class friendship.

Chapter 5 on C++ Templates for Microcontrollers covers the basics of templates with some examples in the context of microcontrollers. 

Interestingly, Chapter 6 is titled Optimized C++ Programming for Microcontrollers, and contains 20 tips for writing good code. While some of the tips make sense (and some of them have no impact on performance), I was stumped by two tips in the list:

6.8: Use Comments Sparingly. 

6.18: Use Lambda Expressions. 

Regarding 6.8, I don’t know how many would approve of this tip! Also, what impact does this have on performance? And what about 6.18? The reason the author gives is that using lambdas can result in considerable code saving and can improve performance. Well, this is not always true. See this discussion. It would have been better if the author had given specific scenarios where lambdas make sense and where they do not, instead of making a sweeping recommendation in their favour. Having said that, however, tips such as Use Native Integer Types (6.10) and Consider ROM-ability (6.14) definitely make sense.

In Chapter 7, the author explains how to write C++ code to access hardware addresses, especially the port addresses. He prefers to use a template class for this purpose. There is nothing wrong with it, but I would prefer implementing using non-member template functions.  

Chapter 8 describes how to write the startup code that runs after the hardware is reset, but before main gets control. This is a non-trivial task and the author shows how to do this in C++ and partly in assembly. Along the way, he shares interesting insights on the linker definition file. 

I personally found Chapter 9 interesting. There is some good discussion about writing low-level drivers in C++ with many examples.

Chapter 10 discusses custom memory management. Dynamic memory management can be risky/challenging in microcontroller programming because of limited memory constraints. In this chapter, the author shows how to overload new and delete operators to take control over dynamic memory management. In the last section of the chapter, there is a brief note on using exception handling to recover from memory errors, and the author correctly observes that this has to be done with care because of the overhead involved.

Chapter 11 is on multitasking in C++. It begins with an overview of task scheduling and concludes with a mention of the C++ Threading library.  

In chapters 12 to 15, the author discusses various topics such as fixed-point and floating-point arithmetic, implementing digital filters, and some utility classes that are useful in embedded programming. 

Chapter 16 is about simple extensions to the STL and replacements when the target environment does not provide the required functionality.

Chapter 17 talks about using C code in C++, a fairly common scenario because a lot of useful embedded code is still written in C.

Appendix A is titled A Tutorial for Real-Time C++. It is just a summary of C++17 features with no mention of anything remotely connected to real-time. Even Appendix B (A Robust Real-Time C++ Environment) has only a few paragraphs on interrupts and timing. The rest of it is pretty general stuff.

Read this book if you want to get an overview of embedded programming for microcontrolers in C++. However, be prepared to be bombarded with C++17 features throughout! If your focus is Real-time programming in C++, then this book might disappoint you. I would have really enjoyed the book if the author had given a balanced perspective of C++17 features – what to use and when, and more importantly what not to use.

Have a nice weekend!

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *