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 pagine
  2. English
  3. ePUB (disponibile sull'app)
  4. Disponibile su iOS e Android
eBook - ePub

Embedded Programming with Modern C++ Cookbook

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

Igor Viarheichyk

Dettagli del libro
Anteprima del libro
Indice dei contenuti
Citazioni

Informazioni sul libro

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.

Domande frequenti

Come faccio ad annullare l'abbonamento?
È semplicissimo: basta accedere alla sezione Account nelle Impostazioni e cliccare su "Annulla abbonamento". Dopo la cancellazione, l'abbonamento rimarrà attivo per il periodo rimanente già pagato. Per maggiori informazioni, clicca qui
È possibile scaricare libri? Se sì, come?
Al momento è possibile scaricare tramite l'app tutti i nostri libri ePub mobile-friendly. Anche la maggior parte dei nostri PDF è scaricabile e stiamo lavorando per rendere disponibile quanto prima il download di tutti gli altri file. Per maggiori informazioni, clicca qui
Che differenza c'è tra i piani?
Entrambi i piani ti danno accesso illimitato alla libreria e a tutte le funzionalità di Perlego. Le uniche differenze sono il prezzo e il periodo di abbonamento: con il piano annuale risparmierai circa il 30% rispetto a 12 rate con quello mensile.
Cos'è Perlego?
Perlego è un servizio di abbonamento a testi accademici, che ti permette di accedere a un'intera libreria online a un prezzo inferiore rispetto a quello che pagheresti per acquistare un singolo libro al mese. Con oltre 1 milione di testi suddivisi in più di 1.000 categorie, troverai sicuramente ciò che fa per te! Per maggiori informazioni, clicca qui.
Perlego supporta la sintesi vocale?
Cerca l'icona Sintesi vocale nel prossimo libro che leggerai per verificare se è possibile riprodurre l'audio. Questo strumento permette di leggere il testo a voce alta, evidenziandolo man mano che la lettura procede. Puoi aumentare o diminuire la velocità della sintesi vocale, oppure sospendere la riproduzione. Per maggiori informazioni, clicca qui.
Embedded Programming with Modern C++ Cookbook è disponibile online in formato PDF/ePub?
Sì, puoi accedere a Embedded Programming with Modern C++ Cookbook di Igor Viarheichyk in formato PDF e/o ePub, così come ad altri libri molto apprezzati nelle sezioni relative a Informatik e Programmierung in C++. Scopri oltre 1 milione di libri disponibili nel nostro catalogo.

Informazioni

Anno
2020
ISBN
9781838823207
Edizione
1
Argomento
Informatik

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...

Indice dei contenuti