4 different ways to use sleep() in C++
With examples of portability for Linux and Windows.
Making a code wait for a certain time is a practice used by many programmers.
We will show 5 examples in each of them for the code to wait 2 seconds in some cases also using microseconds and milliseconds .
This is perhaps the simplest form of all, using the
#include <unistd.h> header
For more information run the command:
std::chrono is a flexible collection of types that track time with varying degrees of precision. For this example we will use the function:
Entering the time in seconds:
Reporting the time in microseconds
It is still possible to use:
milliseconds and among others.
Only for Windows if you want to create portable solutions, it would be something like this:
4º Using the
Check first if you have it installed on your system, although I find it difficult not to have it, as many things use it.
For this example we use
To compile use the
-pthread flags together, example:
That’s all for today, small daily doses that will always keep us tuned with C++ !