tags:

views:

512

answers:

8

Does c/c++ have a delay/wait/sleep function?

A: 

sleep()? Dunno, just an idea.

wishi
i tried, c/c++ doesn't have sleep()
My understanding is that `sleep()` is not available for all platforms.
Dominic Rodger
+2  A: 

Depends on the platform. There're sleep() and usleep() for instance.

Michael Krelin - hacker
which platform is that? which header is the function defined? thx
See links. Wouldn't it make more sense if you tell us what is your platform?
Michael Krelin - hacker
+4  A: 

The closest to a cross-platform sleep function that I know of is in boost::thread. It's called sleep.

However, if you're working on a platform where plain ol' sleep(unsigned int seconds) is available, then I'd just use that.

Dominic Rodger
A: 

I asked the mighty google for it and its first answer goes like:

int x,y;
for(x = 0; x < 2000; x++)
{
    for(y = 0; y < 2000000; y++)
    {
    }
}

But you have to adjust the number in for loop to your system.

time_t start_time, cur_time;
time(&start_time);
do
{
    time(&cur_time);
}
while((cur_time - start_time) < 3);

and

clock_t start_time, cur_time;
start_time = clock();
while((clock() - start_time) < 3 * CLOCKS_PER_SEC)
{
}

and last but not least

There are several other functions that can be used. For Windows computers, there are _ftime, Sleep(), and QueryPerformanceCounter() functions, as well as the sytem timer.

DaClown
(it wasn't me who hit this with the -1). The first snippet in this is a really bad way to make a delay since a) it will delay for different times on different processors and b) compiling with optimisations on will likely detect (correctly) that it does nothing and skip straight over it.
Kaz Dragon
You are right, it just what google gave me. I remember using it in Turbo Pascal about 10 years ago and it worked fine ... on my machine :) Also it is a hint to you google, espacially because of the OPs user name.
DaClown
It's also a bad thing because it will not sleep, it will work hard, smoking your cpu and not letting other processes take advantage of your sleep.
Michael Krelin - hacker
But the question is for a `delay function` and not a sleep function. Also this loop ways are platform independet and the OP didn't specify an OS
DaClown
+5  A: 

C++ does not have a sleep function. But most platforms do.

On Linux you have sleep() and usleep(). On Windows you have Sleep().

You just have to include the appropriate headers to get access to them.

jalf
that's what i thought. i couldn't find one in the c/c++ libs
A: 

There is no cross platform solution. sleep() is POSIX, not standard C/C++.

You could look at a cross platform library that provides sleep, e.g. SDL.

Source code.

Nick
A: 

aha, you can use Sleep() in windows.it's a kernel function, in linux it is sleep(x) x = mil-sec

Macroideal
A: 

sleep is not very accurate, as it only give you seconds granularity and your process might not wake up right on the "dot". If you want much more accurate timer. I would use select system call. both unix and windows have it.

Something like this will sleep for 10 microseconds struct timeval tv;

tv.sec = 0;
tv.tv_usec = 10;
select(0,NULL,NULL,NULL,&tv);

discovlad