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++?
708
votes
18 Answers
497
votes
1430
votes
84
votes
To stay portable you could use Boost::Thread for sleeping:
#include <boost/thread/thread.hpp>
int main()
{
//waits 2 seconds
boost::this_thread::sleep( boost::posix_time::seconds(1) );
boost::this_thread::sleep( boost::posix_time::milliseconds(1000) );
return 0;
}
This answer is a duplicate and has been posted in this question before. Perhaps you could find some usable answers there too.
33
votes
26
votes
Why don't use time.h library? Runs on Windows and POSIX systems(Don't use this code in production!):
#include <iostream>
#include <time.h>
using namespace std;
void sleepcp(int milliseconds);
void sleepcp(int milliseconds) // Cross-platform sleep function
{
clock_t time_end;
time_end = clock() + milliseconds * CLOCKS_PER_SEC/1000;
while (clock() < time_end)
{
}
}
int main()
{
cout << "Hi! At the count to 3, I'll die! :)" << endl;
sleepcp(3000);
cout << "urrrrggghhhh!" << endl;
}
Corrected code - now CPU stays in IDLE state [2014.05.24]:
#include <iostream>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif // _WIN32
using namespace std;
void sleepcp(int milliseconds);
void sleepcp(int milliseconds) // Cross-platform sleep function
{
#ifdef _WIN32
Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif // _WIN32
}
int main()
{
cout << "Hi! At the count to 3, I'll die! :)" << endl;
sleepcp(3000);
cout << "urrrrggghhhh!" << endl;
}
17
votes
14
votes
9
votes
7
votes
6
votes
On platforms with the select
function (POSIX, Linux, and Windows) you could do:
void sleep(unsigned long msec) {
timeval delay = {msec / 1000, msec % 1000 * 1000};
int rc = ::select(0, NULL, NULL, NULL, &delay);
if(-1 == rc) {
// Handle signals by continuing to sleep or return immediately.
}
}
However, there are better alternatives available nowadays.
4
votes
The way to sleep your program in C++ is the Sleep(int)
method. The header file for it is #include "windows.h."
For example:
#include "stdafx.h"
#include "windows.h"
#include "iostream"
using namespace std;
int main()
{
int x = 6000;
Sleep(x);
cout << "6 seconds have passed" << endl;
return 0;
}
The time it sleeps is measured in milliseconds and has no limit.
Second = 1000 milliseconds
Minute = 60000 milliseconds
Hour = 3600000 milliseconds
3
votes
0
votes
0
votes
Sleep()
has millisecond precision, but it's accuracy can be orders of magnitude higher. You may think your sleeping for 4 milliseconds, but actually sleep for 400. – John Diblingsleep
, not win32Sleep
given "x seconds". – CB Bailey#include
the C header inside anextern "C" {}
block. Also, if you have C and C++ source files in the same project, it's highly recommended that you do this in order to avoid any problems, especially if you include the same headers in both kinds of source files (in which case this is necessary). If you have a purely C++ project, it might just work with no problem at all. – notadamGetTickCount
had 55ms resolution; later versions had 16ms resolution or less. One user thought he was getting 16ms resolution from Sleep but then reported thatSleep
itself was fairly accurate, and the apparent imprecision was caused by usingGetTickCount
to measure the passage of time. – Qwertie