tags:

views:

33

answers:

2

i have an application with 4 threads from which 2 are event based and 2 are not event based.

the problem is i have to isolate the 2 non event based threads in which while(1) loop is executing that takes a huge cpu usage and this usage reaches even up to 100%,

i think making these threads isolated can reduce the cpu usage,

will it be a good idea, if there is any other method plz refer me.

and i also want to know how to isolate the threads.

thanx in advance.

+1  A: 

If you make your threads sleep for a short amount of time after each loop, that should reduce CPU load.

Duracell
+1  A: 

As a general rule, you rarely want to simply reduce the CPU usage. While you certainly can do so (e.g., by sleeping for a while on a semi-regular basis) it won't really accomplish much. Assuming, of course, that the threads are doing something useful with the CPU time, reducing CPU usage will simply stretch the computation over a longer period of time.

To derive real benefit, reduce the priority of the CPU-intensive threads. This allows the threads to finish their computation as quickly as possible as long as they're the only threads that are ready to run. At the same time, it also means that the lower-priority threads will consume absolutely no CPU time1 as long as there are any higher-priority threads that are ready to run.

  1. Well, if you want to get technical, Windows has an anti-starvation mechanism that lets lower priority threads that are ready to run get a minuscule bit of CPU time, but it's so little that it normally won't affect higher-priority threads enough to notice.
Jerry Coffin
you want to say simply give the threads low priority ?
moon
@moon: yes, that's pretty much it, unless (of course) you really need to isolate them for some other reason (e.g., they crash, and you want to be able to kill and re-start them independent of the rest of the application -- but the right way to cure that is to stop them from crashing).
Jerry Coffin
@Jerry: What if the non-event based threads are supposed to be doing higher priority tasks?
puffadder
@puffadder: That's *extremely* rare. You want to do processing as fast as reasonable -- but remaining responsive to user input essentially *always* takes priority. Just for example, continuing to process after the user attempts to cancel the processing is virtually never a good idea.
Jerry Coffin