Time

R

Rasputin

Is there any way to wait for a particular amount
of time without depending on machine cycles etc?
 
T

Thomas Matthews

Rasputin said:
Is there any way to wait for a particular amount
of time without depending on machine cycles etc?

Yes, and no.
Yes, you can get the time add to it, then wait in
a loop until the time reaches the new value. Research
the "clock" and "time" functions.

If you want fine resolution, you will have to use
an operating system function to retrieve the time.

Many real-time operating systems or even multi-
tasking operating systems have the ability to
put your program to sleep (block it) until an
event occurs, such as a timer tick or a duration
expires. This allows other tasks in the system
to execute while yours is sleeping versus your
programming hogging up CPU cycles waiting for
a period of time and not letting other tasks
execute. (Some smarter operating systems may
swap other higher priority tasks in and out
of memory while yours is doing nothing).

Search a newsgroup related to your operating
system or platform for more information.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
P

Peter Pichler

Rasputin said:
Is there any way to wait for a particular amount
of time without depending on machine cycles etc?

In standard C? You can wait in a loop until time() returns a different
value. That's about it. And you are not guaranteed how often that happens.
For anything "better" (for any definition of the word), use some platform
extensions <OT>such as sleep(), wait() etc</OT>.

Peter
 
R

Rasputin

In standard C? You can wait in a loop until time() returns a different
value. That's about it. And you are not guaranteed how often that happens.
For anything "better" (for any definition of the word), use some platform
extensions <OT>such as sleep(), wait() etc</OT>.

Peter

Thanks I found sleep. Sorry I should've looked into it
more i guess before posting here.
 
R

Rasputin

This version consumes uber amount of cpu.
unistd.h is much better.

#include <stdio.h>
#include <time.h>

void sleep( time_t delay );

int main()
{
while(1) {
puts( "Delaying for 3 seconds." );
sleep( 3 );
puts( "Done!" );
}

return 0;
}

void sleep( time_t delay )
{
time_t t0, t1;
time( &t0 );
do
{
time( &t1 );
}
while (( t1 - t0 ) < delay );
}
 
K

Keith Thompson

Rasputin said:
void sleep( time_t delay )
{
time_t t0, t1;
time( &t0 );
do
{
time( &t1 );
}
while (( t1 - t0 ) < delay );
}

Apart from, as you pointed out, unnecessarily consuming lots of CPU
time, this is non-portable. It assumes that subtracting a time_t from
a time_t yields a meaningful time_t result representing the number of
seconds between the two times. In fact, the standard's only guarantee
is that time_t is an arithmetic type capable of representing times;
there's no guarantee that it's integer as opposed to floating-point,
that it's signed as opposed to unsigned, that the granularity is one
second, or even that times are represented monotonically. (As it
happens, most of these assumptions happen to be true on most existing
systems.) If you want to write completely portable code, you need to
assume that time_t values are as opaque as pointers.

A more portable version of the above function would use the difftime()
function to compute the difference, and the delay parameter would be a
double, not a time_t. I'm not going to post a corrected version
because I'm afraid someone might actually try to use it, perhaps even
on a system that I'm trying to use at the same time. :cool:}

If you're not concerned about 100% portability, you might as well use
sleep() or whatever equivalent your system provides.

(It wouldn't have been unreasonable, in my opinion, for the sleep()
function to have been included in the C standard, but it wasn't.)
 
M

Malcolm

Keith Thompson said:
(It wouldn't have been unreasonable, in my opinion, for the sleep()
function to have been included in the C standard, but it wasn't.
You're right. On any sort of multi-user / mutli-threaded program busy idling
is an unspeakable sin.
 
K

Keith Thompson

Malcolm said:
You're right. On any sort of multi-user / mutli-threaded program busy idling
is an unspeakable sin.

On the other hand, any system that provides the extensions necessary
to support multi-threading, or even just multiple users or processes,
will certainly provide an appropriate sleep() function. There's
little need for sleep() in portable code; non-portable code might as
well use a non-portable sleep() function.

The alternative would have been to include such support in the C
standard, but that was almost certainly impractical.
 
C

CBFalconer

Keith said:
On the other hand, any system that provides the extensions necessary
to support multi-threading, or even just multiple users or processes,
will certainly provide an appropriate sleep() function. There's
little need for sleep() in portable code; non-portable code might as
well use a non-portable sleep() function.

I disagree. Many single thread programs need a delay mechanism,
for which 'sleep' is a fine substitute. Some example usages:
Timing out interactive queries, allowing 'action' time for dumb
peripherals. The time resolution is a QOI factor, but not
normally critical.

The things that work with the sleep will very often be
non-portable.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top