Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Sleep for milliseconds

I know the POSIX sleep(x) work makes the program sleep for x seconds. Is there a function to make the program sleep for x milliseconds in C++?
by

2 Answers

espadacoder11
Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for usleep, which accepts microseconds:

#include <unistd.h>

unsigned int microseconds;
...
usleep(microseconds);
kshitijrana14
In C++11, you can do this with standard library facilities:
#include <chrono>
#include <thread>

std::this_thread::sleep_for(std::chrono::milliseconds(x));

Clear and readable, no more need to guess at what units the sleep() function takes.

Login / Signup to Answer the Question.