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

Compartir libro
  1. 412 páginas
  2. English
  3. ePUB (apto para móviles)
  4. Disponible en iOS y Android
eBook - ePub

Embedded Programming with Modern C++ Cookbook

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

Igor Viarheichyk

Detalles del libro
Vista previa del libro
Índice
Citas

Información del 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.

Preguntas frecuentes

¿Cómo cancelo mi suscripción?
Simplemente, dirígete a la sección ajustes de la cuenta y haz clic en «Cancelar suscripción». Así de sencillo. Después de cancelar tu suscripción, esta permanecerá activa el tiempo restante que hayas pagado. Obtén más información aquí.
¿Cómo descargo los libros?
Por el momento, todos nuestros libros ePub adaptables a dispositivos móviles se pueden descargar a través de la aplicación. La mayor parte de nuestros PDF también se puede descargar y ya estamos trabajando para que el resto también sea descargable. Obtén más información aquí.
¿En qué se diferencian los planes de precios?
Ambos planes te permiten acceder por completo a la biblioteca y a todas las funciones de Perlego. Las únicas diferencias son el precio y el período de suscripción: con el plan anual ahorrarás en torno a un 30 % en comparación con 12 meses de un plan mensual.
¿Qué es Perlego?
Somos un servicio de suscripción de libros de texto en línea que te permite acceder a toda una biblioteca en línea por menos de lo que cuesta un libro al mes. Con más de un millón de libros sobre más de 1000 categorías, ¡tenemos todo lo que necesitas! Obtén más información aquí.
¿Perlego ofrece la función de texto a voz?
Busca el símbolo de lectura en voz alta en tu próximo libro para ver si puedes escucharlo. La herramienta de lectura en voz alta lee el texto en voz alta por ti, resaltando el texto a medida que se lee. Puedes pausarla, acelerarla y ralentizarla. Obtén más información aquí.
¿Es Embedded Programming with Modern C++ Cookbook un PDF/ePUB en línea?
Sí, puedes acceder a Embedded Programming with Modern C++ Cookbook de Igor Viarheichyk en formato PDF o ePUB, así como a otros libros populares de Informatik y Programmierung in C++. Tenemos más de un millón de libros disponibles en nuestro catálogo para que explores.

Información

Año
2020
ISBN
9781838823207
Edición
1
Categoría
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...

Índice