April 2020
Intermediate to advanced
412 pages
9h 58m
English
In our application, we created two functions, sleep_for and sleep_until. They are almost identical, except sleep_for uses std::this_thread::sleep_for to add a delay, while sleep_until uses std::this_thread::sleep_until.
Let's take a closer look at the sleep_for function. It takes two parameters—count and delay. The first parameter defines a number of iterations in its loop, and the second parameter specifies a delay. We use auto as a data type of the delay parameter, letting C++ infer the actual data type for us.
The function body consists of a single loop:
for (int i = 0; i < count; i++) {
On each iteration, we run the delay and measure its actual duration by taking timestamps before and after the delay. The std::this_thread::sleep_for ...