How to make a delay (sleep) in C++ for several milliseconds?
Hello! I'm writing a simple C++ program on Linux, and I need to make a pause of several milliseconds. I'm a beginner, and so far I only know about the `sleep()` function from ``, but it seems to work only with seconds. I need something like "wait 500 milliseconds".
Here's an example of my code, which of course doesn't compile because I don't know the correct function:
```cpp
#include
#include // there is sleep() here
int main() {
std::cout << "Starting..." << std::endl;
sleep(500); // Error! sleep waits 500 seconds, not milliseconds
std::cout << "Did 500 milliseconds pass?" << std::endl;
return 0;
}
```
I'm using Linux (Ubuntu), compiling with g++. I tried googling, but found many different options: `usleep`, `nanosleep`, `std::this_thread::sleep_f
No answers yet.
Log in to leave an answer