By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,497 Members | 2,483 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,497 IT Pros & Developers. It's quick & easy.

Doing short pauses

P: n/a
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?

Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Michel Rouzic wrote:
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

Nov 16 '05 #2

P: n/a

David Resnick wrote:
Michel Rouzic wrote:
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


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.

Nov 16 '05 #3

P: n/a

Michel Rouzic wrote:
David Resnick wrote:
Michel Rouzic wrote:
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


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

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.