--- In [email protected], Dave Van den Eynde <[EMAIL PROTECTED]> wrote:
> I replaced the functions msDelay() and usDelay() with the following:
> 
> void msDelay(int ms)
> {
>   struct timeval tv;
> 
>   tv.tv_sec = 0;
>   tv.tv_usec = ms * 1000;
> 
>   select(0, NULL, NULL, NULL, &tv);
> }
> 
> void usDelay(int us)
> {
>   struct timeval tv;
> 
>   tv.tv_sec = 0;
>   tv.tv_usec = us;
> 
>   select(0, NULL, NULL, NULL, &tv);
> }
> 

This is indeed the best way to handle "small" sleeps.  I've
experienced the same a few weeks back: 95% of the CPU time was taken
by my application because there was no "decent" wait routine.

Using select()is the solution to solve this.

You can also use the function poll(), that's an alternative that 
uses less "complicated" time structures.  In fact, you simply have to
give a time (in ms) as time-out for the function.  Here's the prototype:

int poll(struct pollfd *fds, nfds_t nfds, int timeout);

Also here, simply put the unnecessary params to 0 or NULL and only use
"timeout" to define the amount of time this function is given before
it should return (read: how long is it allowed to "sleep").

The result is a drop from 95% CPU usage to almost nothing (the "top"
function on the FoxBoard shows me 0.0%...).

I have, however, experienced that the smallest time I could set with
select() and/or poll(), was 10ms.

I guess that's the "timer tick" of the whole system and I doubt if one
can switch to a smaller time-out time, which is still accurate.

Next to this, since my "main" function is an endless loop (I'm using
the "main" to start my threads and then I don't need it any more in
fact), I've added a "rough" sleep( 1 ); into my while() function
(needed to prevent "main" from ending), again to avoid (almost) 100%
CPU usage of my application.

That works perfect.


Best rgds,

--Geert


Reply via email to