Doing short pauses

M

Michel Rouzic

I got a loop checking if the system time has took one more second or
not since last iteration, works nicely, but it uses the whole CPU.

I've read many previous topics on c.l.c about wait() type functions but
I want standard C. I'm just trying to relieve the CPU by making a short
pause in each iteration of the loop, so my loop iterates a few
thousand/million times less often, is there any portable way of doing
it?
 
D

David Resnick

Michel said:
I got a loop checking if the system time has took one more second or
not since last iteration, works nicely, but it uses the whole CPU.

I've read many previous topics on c.l.c about wait() type functions but
I want standard C. I'm just trying to relieve the CPU by making a short
pause in each iteration of the loop, so my loop iterates a few
thousand/million times less often, is there any portable way of doing
it?

http://www.eskimo.com/~scs/C-faq/q19.37.html

-David
 
M

Michel Rouzic

David said:

yeah but, I was just wondering if there wasn't some function that could
be called which purpose isn't even to do pauses, but that would take
some time to execute without takin all the CPU while doing this, just
like I noticed that when printing data to the screen it makes the
program execute much slower as the final CPU time when the program is
about to end is the same.
 
D

David Resnick

Michel said:
yeah but, I was just wondering if there wasn't some function that could
be called which purpose isn't even to do pauses, but that would take
some time to execute without takin all the CPU while doing this, just
like I noticed that when printing data to the screen it makes the
program execute much slower as the final CPU time when the program is
about to end is the same.

Sounds like the wrong approach. On the one hand, you are trying to be
portable, but on the other you are counting on a behavior that depends
on the details of the implementation and could change when you upgrade
libraries/etc. Yes, you might be able to use less CPU if you write a 1
k
block repeatedly to disk (being I/O bound perhaps), but seems like
the wrong approach.

A better idea might be to implement something like this:

const char *system_sleep_cmd =
read_from_config("SYSTEM_SLEEP_COMMAND");
if (system_sleep_cmd != NULL) {
system(system_sleep_cmd);
}

Assuming you have some mechanism to read configuration or some such.
It could be the string "sleep 5" on one system, something else on
another...

Or to have a function in your "platform dependant" section of your
program
compat_sleep or some such that uses the approprate system dependant
mechanism to sleep... That could be made quite portable by using
the busy loop as the last resort if your series of #ifdefs peters out.

-David
 

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

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,474
Latest member
VivianStuk

Latest Threads

Top