Re: Continuous loop handling

From: Jerry Coffin (jcoffin_at_taeus.us)
Date: 04/25/04


Date: Sun, 25 Apr 2004 10:47:53 -0600

In article <c6gl4u$6lr$1@news4.jaring.my>, jamesg@pd.jaring.my
says...
> Hi Group
>
> I am executing a function in my program each time there is a external
> hardware trigger. Currently I am using a worker thread to continuously
> monitor the hardware input in a while loop. Tough my gui is active by this
> mechanism, but my cpu usage time is always 100%. and my GUI does get a
> little sluggish.
>
> How to improve this mechanism, I feel it is not efficient to let my cpu time
> be used 100% by my program. Is there a way to free up the cpu time
> especially during
> the ..wait for hardware trigger to change '0' loop.

Ideally, the device driver for your hardware would do something like
signal an Event when the input takes place, even just provide a file
for you to read from that'll block until there's data to read.

Barring that, you've got a couple of choices. One would be to call
Sleep somewhere inside your loop. The number you pass as the argument
is (roughly) the number of milliseconds before your loop will
continue, and normally works in 10 ms increments, so if you can allow
a change to go unnoticed for around 100 ms, Sleep(100) will do that,
and reduce your CPU load to nearly 0. This will make the thread in
which you're doing the checking take less CPU time, but if it's the
same thread as you're using to implement your GUI, it'll cause a
problem: during the Sleep time, you can't process messages either, so
while this will reduce CPU usage, it'll probably make the GUI of that
program even LESS responsive (though it'll make other programs far
more responsive).

If you're doing this in the same thread as the GUI, it's probably
better not to check the value in an explicit loop at all. Instead,
set a time for a reasonable interval and check the value in response
to the timer -- and then if it's changed, you usually want to post
another message back to your own queue saying there's something to
process.

Whether that's a good solution or not depends heavily on how quickly
you need to respond to the hardware event -- timers aren't
necessarily particularly prompt, and posting a message back to your
own queue CAN result in a fairly long (e.g. 100 ms) delay as well.
One way to help is to use a multimedia timer instead -- you can
usually expect them to respond within a ms of when you ask for.

Another possibility would be to move the code into a separate thread.
This isn't a panacea, but it can make it a lot easier to govern how
much CPU usage is devoted to that task. For one example, you can set
the loop to run like you're doing right now, but set the thread to
lowest priority. This means the thread will get all the CPU time as
long as there's nothing else ready to execute, but no CPU time if
anything else can run.

-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.


Relevant Pages

  • Re: Waiting in a loop
    ... Around 15000 values are sent to the hardware. ... I have a while loop ... the routine by clicking a cancel button in the gui. ... How can I setup my waiting routine so ...
    (comp.soft-sys.matlab)
  • Continuous loop handling
    ... Tough my gui is active by this ... I feel it is not efficient to let my cpu time ... the ..wait for hardware trigger to change '0' loop. ...
    (microsoft.public.vc.mfc)
  • Re: Changing process priority
    ... How do I make sure a simple whileloop ... doesn't consume half my CPU? ... Though sometimes there is a "idle process" that runs ... an infinite loop consuming all leftover CPU time, ...
    (comp.lang.c)
  • Re: Linux-next Alsa hda_intel events/0 high CPU usage, bisected
    ... I didn't expect that this delay is so much, (IOW, the hardware is so ... I recompiled the g8a4bd4d kernel with that option off and a few more ... This will show some latency time in the kernel messages. ... I don't know if a high CPU time for the events/0 process is ...
    (Linux-Kernel)
  • Re: How Do I Use An SSLServerSocket With ServerSocketChannel?
    ... it's Java 5.0 or give up. ... endless loops of reading a socket and writing to the other one while ... loop and when it does a ServerSocket.accept, it goes into the inner loop ... isn't that going to steal a lot of CPU time ...
    (comp.lang.java.help)

Quantcast