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
The way to sleep your program in C++ is the
Sleep(int)
method. The header file for it is#include "windows.h."
For example:
The time it sleeps is measured in milliseconds and has no limit.
As a Win32 replacement for POSIX systems:
In Unix you can use usleep.
In Windows there is Sleep.
nanosleep
is a better choice thanusleep
- it is more resilient against interrupts.If using MS Visual C++ 10.0, you can do this with standard library facilities:
you will need:
Why don't use time.h library? Runs on Windows and POSIX systems:
Corrected code - now CPU stays in IDLE state [2014.05.24]: