Book Image

C++ Programming for Linux Systems

By : Desislav Andreev, Stanimir Lukanov
Book Image

C++ Programming for Linux Systems

By: Desislav Andreev, Stanimir Lukanov

Overview of this book

Around 35 million Linux and almost 2 billion Android users rely on C++ for everything from the simplest embedded and IoT devices to cloud services, supercomputing, and space exploration. To help you produce high-quality software, two industry experts have transformed their knowledge and experience into practical examples in system programming with C++ Programming for Linux Systems. In this book, you'll explore the latest C++20 features, while working on multiple specific use cases. You’ll get familiar with the coroutines and modern approaches in concurrent and multithreaded programming. You'll also learn to reshape your thinking when analyzing system behavior in Linux (POSIX) environments. Additionally, you'll discover advanced discussions and novel solutions for complex challenges, while approaching trivial system operations with a new outlook and learning to choose the best design for your particular case. You can use this workbook as an introduction to system programming and software design in Linux or any Unix-based environment. You’ll also find it useful as a guideline or a supplement to any C++ book. By the end of this book, you’ll have gained advanced knowledge and skills for working with Linux or any Unix-based environment.
Table of Contents (15 chapters)
Free Chapter
1
Part 1:Securing the Fundamentals
7
Part 2:Advanced Techniques for System Programming

Using clocks, timers, and ratios

Before getting into more examples with clocks and timers, we first have to get a better understanding of how the chrono library defines a duration.

As we saw in the previous example, a duration is the distance between two points of time, called timepoints. In our previous example, these were the start and end timepoints.

Figure 8.1 – Timepoint and duration

Figure 8.1 – Timepoint and duration

The duration itself is a combination of the count of ticks and a fraction that represents the time in seconds from one tick to the next. The fraction is represented by the std::ratio class. Here are some examples:

using namespace std::chrono;
constexpr std::chrono::duration<int, std::ratio<1>>
  six_minutes_1{360};
constexpr std::chrono::duration<double, std::ratio<3600>>
  six_minutes_2{0.1};
constexpr std::chrono::minutes six_minutes_3{6};
constexpr auto six_minutes_4{6min};
std::cout << six_minutes_1 &lt...