Periodic data processing is a common pattern in many embedded applications. The code does not need to work all the time. If we know in advance when processing is needed, an application or a worker thread can be inactive most of the time, waking up and processing data only when needed. It saves power consumption or lets other applications running on the device use the CPU resources when the application is idle.
There are several techniques to organize periodic processing. A worker thread that runs a loop with a delay in it is one of the simplest and most common of them.
C++ provides standard functions to add a delay to the current execution thread. In this recipe, we will learn two ways of adding a delay into an application and discuss their pros and cons.