Rasputin <someone@neverland.net> writes:
[...][color=blue]
> void sleep( time_t delay )
> {
> time_t t0, t1;
> time( &t0 );
> do
> {
> time( &t1 );
> }
> while (( t1 - t0 ) < delay );
> }[/color]
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. 8-)}
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.)
--
Keith Thompson (The_Other_Keith)
kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"