Embedded Programming with Modern C++ Cookbook
eBook - ePub

Embedded Programming with Modern C++ Cookbook

Practical recipes to help you build robust and secure embedded applications on Linux

Igor Viarheichyk

  1. 412 pages
  2. English
  3. ePUB (adapté aux mobiles)
  4. Disponible sur iOS et Android
eBook - ePub

Embedded Programming with Modern C++ Cookbook

Practical recipes to help you build robust and secure embedded applications on Linux

Igor Viarheichyk

DĂ©tails du livre
Aperçu du livre
Table des matiĂšres
Citations

À propos de ce livre

Explore various constraints and challenges that embedded developers encounter in their daily tasks and learn how to build effective programs using the latest standards of C++

Key Features

  • Get hands-on experience in developing a sample application for an embedded Linux-based system
  • Explore advanced topics such as concurrency, real-time operating system (RTOS), and C++ utilities
  • Learn how to test and debug your embedded applications using logs and profiling tools

Book Description

Developing applications for embedded systems may seem like a daunting task as developers face challenges related to limited memory, high power consumption, and maintaining real-time responses. This book is a collection of practical examples to explain how to develop applications for embedded boards and overcome the challenges that you may encounter while developing.

The book will start with an introduction to embedded systems and how to set up the development environment. By teaching you to build your first embedded application, the book will help you progress from the basics to more complex concepts, such as debugging, logging, and profiling. Moving ahead, you will learn how to use specialized memory and custom allocators. From here, you will delve into recipes that will teach you how to work with the C++ memory model, atomic variables, and synchronization. The book will then take you through recipes on inter-process communication, data serialization, and timers. Finally, you will cover topics such as error handling and guidelines for real-time systems and safety-critical systems.

By the end of this book, you will have become proficient in building robust and secure embedded applications with C++.

What you will learn

  • Get to grips with the fundamentals of an embedded system
  • Understand how to optimize code for the targeted hardware platforms
  • Explore cross-compilation, build types, and remote debugging
  • Discover the importance of logging for debugging and root cause analysis of failures
  • Uncover concepts such as interrupt service routine, memory model, and ring buffer
  • Recognize the need for custom memory management in embedded systems
  • Delve into static code analyzers and tools to improve code quality

Who this book is for

This book is for developers, electronic hardware professionals, and software and system-on-chip engineers who want to build effective embedded programs in C++. Familiarity with the C++ programming language is expected, but no previous knowledge of embedded systems is required.

Foire aux questions

Comment puis-je résilier mon abonnement ?
Il vous suffit de vous rendre dans la section compte dans paramĂštres et de cliquer sur « RĂ©silier l’abonnement ». C’est aussi simple que cela ! Une fois que vous aurez rĂ©siliĂ© votre abonnement, il restera actif pour le reste de la pĂ©riode pour laquelle vous avez payĂ©. DĂ©couvrez-en plus ici.
Puis-je / comment puis-je télécharger des livres ?
Pour le moment, tous nos livres en format ePub adaptĂ©s aux mobiles peuvent ĂȘtre tĂ©lĂ©chargĂ©s via l’application. La plupart de nos PDF sont Ă©galement disponibles en tĂ©lĂ©chargement et les autres seront tĂ©lĂ©chargeables trĂšs prochainement. DĂ©couvrez-en plus ici.
Quelle est la différence entre les formules tarifaires ?
Les deux abonnements vous donnent un accĂšs complet Ă  la bibliothĂšque et Ă  toutes les fonctionnalitĂ©s de Perlego. Les seules diffĂ©rences sont les tarifs ainsi que la pĂ©riode d’abonnement : avec l’abonnement annuel, vous Ă©conomiserez environ 30 % par rapport Ă  12 mois d’abonnement mensuel.
Qu’est-ce que Perlego ?
Nous sommes un service d’abonnement Ă  des ouvrages universitaires en ligne, oĂč vous pouvez accĂ©der Ă  toute une bibliothĂšque pour un prix infĂ©rieur Ă  celui d’un seul livre par mois. Avec plus d’un million de livres sur plus de 1 000 sujets, nous avons ce qu’il vous faut ! DĂ©couvrez-en plus ici.
Prenez-vous en charge la synthÚse vocale ?
Recherchez le symbole Écouter sur votre prochain livre pour voir si vous pouvez l’écouter. L’outil Écouter lit le texte Ă  haute voix pour vous, en surlignant le passage qui est en cours de lecture. Vous pouvez le mettre sur pause, l’accĂ©lĂ©rer ou le ralentir. DĂ©couvrez-en plus ici.
Est-ce que Embedded Programming with Modern C++ Cookbook est un PDF/ePUB en ligne ?
Oui, vous pouvez accĂ©der Ă  Embedded Programming with Modern C++ Cookbook par Igor Viarheichyk en format PDF et/ou ePUB ainsi qu’à d’autres livres populaires dans Computer Science et Programming in C++. Nous disposons de plus d’un million d’ouvrages Ă  dĂ©couvrir dans notre catalogue.

Informations

Année
2020
ISBN
9781838823207
Édition
1

Multithreading and Synchronization

Embedded platforms span a vast landscape of computing power. There are microcontrollers with just a few kilobytes of memory; there are powerful systems-on-chip (SoCs) with gigabytes of memory; there are multi-core CPUs capable of running many applications at the same time.
With more computational resources available for embedded developers, and more complex applications they can build on top of them, multithreading support has become very important. Developers need to know how to parallelize their applications to efficiently utilize all CPU cores. We will learn how to write applications that can utilize all available CPU cores in an efficient and safe way.
In this chapter, we will cover the following topics:
  • Exploring thread support in C++
  • Exploring data synchronization
  • Using condition variables
  • Using atomic variables
  • Using the C++ memory model
  • Exploring lock-free synchronization
  • Using atomic variables in shared memory
  • Exploring async functions and futures
These recipes can be used as examples of building your own efficient multithreading and multiprocessing synchronization code.

Exploring thread support in C++

Prior to C++11, threads were completely out of the scope of C++ as a language. Developers could use platform-specific libraries, such as pthreads or the Win32 application programming interface (API). Since each library has its own behavior, porting applications to another platform required significant development and testing efforts.
C++11 introduced threads as part of the C++ standard and defined a set of classes to create multithreaded applications in its standard library.
In this recipe, we will learn how to use C++ to spawn multiple concurrent threads in a single application.

How to do it...

In this recipe, we will learn how to create two worker threads that run concurrently.
  1. In your ~/test working directory, create a subdirectory called threads.
  2. Use your favorite text editor to create a threads.cpp file in the threads subdirectory. Copy the code snippet into the threads.cpp file:
#include <chrono>
#include <iostream>
#include <thread>

void worker(int index) {
for (int i = 0; i < 10; i++) {
std::cout << "Worker " << index << " begins" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "Worker " << index << " ends" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

int main() {
std::thread worker1(worker, 1);
std::thread worker2(worker, 2);
worker1.join();
worker2.join();
std::cout << "Done" << std::endl;
}
  1. Create a file called CMakeLists.txt in the loop subdirectory, with the following content:
cmake_minimum_required(VERSION 3.5.1)
project(threads)
add_executable(threads threads.cpp)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

SET(CMAKE_CXX_FLAGS "--std=c++11")
target_link_libraries(threads pthread)

set(CMAKE_CXX_COMPILER /usr/bin/arm-linux-gnueabi-g++)
You can build and run the application.

How it works...

In this application, we defined a function called worker. To keep the code simple, it does not do much useful work, only printing Worker X starts and Worker X ends 10 times, with 50 milliseconds' delay between the messages.
In the main function, we create two worker threads, worker1 and worker2:
 std::thread worker1(worker, 1);
std::thread worker2(worker, 2);
We pass two parameters into the thread constructors:
  • A function that runs in the thread.
  • A parameter for the function. Since we pass the previously defined worker function as a thread function, the parameter should match its type—in our case, it is int.
This way, we defined two worker thread that do the same job but have different indices—1 and 2.
The threads start running immediately as soon as they are created; there is no need to call any additional methods to start them. They are executed completely concurrently, as we can see from the program output:
The output from our worker thread is mixed, and sometimes garbled, such as Worker Worker 1 ends2 ends. This happens because output to the Terminal is also working concurrently.
Since worker threads are executed independently, the main thread has nothing to do after creating the worker thread. However, if the execution of the main thread reaches the end of the main function, the program terminates. To avoid this, we added calls to the join method for each of our worker threads. This method blocks until the thread terminates. This way, we exit the main program only after both of the worker threads complete their work.

Exploring data synchronization

Data synchronization is an important aspect of any application that deals with multiple execution threads. Different threads often need to access the same variables or memory regions. Writing to the same memory at the same time by two or more independent threads can result in data corruption. Even reading the variable at the same time when it is being updated by another thread is dangerous, since it can be only partially updated at the moment of the read.
To avoid these issues, concurrent threads can use so-called synchronization primitives, the API that makes access to the shared memory deterministic and predictable.
Similar to the case with thread support, the C++ language did not provide any synchronization primitives prior to the C++11 standard. Starting with C++11, a number of synchronization primitives were added into the C++ standard library as part of the standard.
In this recipe, we will learn how to synchronize access to a variable, using a mutex and a lock guard.

How to do it...

In the preceding recipe, we learned how to run two worker threads completely concurrently and noticed that it can lead to garbled output to the Terminal. We are going to modify the code from the preceding recipe to add synchronization, using a mutex and a lock guard, and see the difference.
  1. In your ~/test working directory, create a subdirectory called mutex.
  2. Use your favorite text editor to create a mutex.cpp file in the mutex subdirectory. Copy the code snippet into the mutex.cpp file:
#include <chrono>
#include <iostream>
#include <mutex>
#include <thread>

std::mutex m;

void worker(int index) {
for (int i = 0; i < 10; i++) {
{
std::lock_guard<std::mutex> g(m);
std::cout << "Worker " << index << " begins" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
std::cout << "Worker " << index << " ends" << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
}

int main() {
std::thread worker1(worker, 1);
std::thread worker2(worker, 2);
worker1.join();
worker2.join();
std::cout << "Done" << std::endl;
}
  1. Create a file called CMakeLists.txt in the loop subdirectory, with the following content:
cmake_minimum_required(VERSION 3.5.1)
project(mutex)
add_executable(mutex mutex.cpp)

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYS...

Table des matiĂšres

  1. Title Page
  2. Copyright and Credits
  3. Dedication
  4. About Packt
  5. Contributors
  6. Preface
  7. Fundamentals of Embedded Systems
  8. Setting Up the Environment
  9. Working with Different Architectures
  10. Handling Interrupts
  11. Debugging, Logging, and Profiling
  12. Memory Management
  13. Multithreading and Synchronization
  14. Communication and Serialization
  15. Peripherals
  16. Reducing Power Consumption
  17. Time Points and Intervals
  18. Error Handling and Fault Tolerance
  19. Guidelines for Real-Time Systems
  20. Guidelines for Safety-Critical Systems
  21. Microcontroller Programming
  22. Other Books You May Enjoy