Sleep for milliseconds

Prasanth Madhavan picture Prasanth Madhavan · Nov 15, 2010 · Viewed 1.4M times · Source

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

Answer

HighCommander4 picture HighCommander4 · May 16, 2012

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.