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++?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
Select call is a way of having more precision (sleep time can be specified in nanoseconds).
Note that there is no standard C API for milliseconds, so (on Unix) you will have to settle for
usleep
, which accepts microseconds:In C++11, you can do this with standard library facilities:
Clear and readable, no more need to guess at what units the
sleep()
function takes.Use Boost asynchronous input/output threads, sleep for x milliseconds;
On platforms with the
select
function (POSIX, Linux, and Windows) you could do:However, there are better alternatives available nowadays.
To stay portable you could use Boost::Thread for sleeping:
This answer is a duplicate and has been posted in this question before. Perhaps you could find some usable answers there too.