Here's the "conventional" way of doing this:
#define INTERVAL 30 /* seconds */
#define MAXLOAD 20 /* hundredths */
while (my loop condition) {
while (notidle())
sleep(INTERVAL);
do my computations;
}
void notidle(void)
{
char buf[4];
int f=open("/proc/loadavg", O_RDONLY);
if ((f<0) || (read(f, buf, 4)!=4))
return -1;
close(f);
return ((buf[0]!='0') || (atoi(buf+2))>MAXLOAD);
}
Granted, it still is a non-beautiful busy loop, but comes rather close
to true idle. Four syscalls every 30 seconds aren't that much. :-)
olaf