Worker thread -> 99% cpu usage..
Hi,
I have a dedicated thread in my program that always polls the parallel port in 1ms intervalls. The problem is that I have it in a continuous while-loop and the cpu usage is going to 99%.
I tried inserting a "sleep(x)" with x being anything between 0-5, but the problem is that sleep uses the system timer and that has a 15ms resolution, so even a sleep(0) can wait up to 15 milliseconds. I used the Performance Counter to verify that.
Is there a way to prevent the thread from hogging all the cpu ?
Re: Worker thread -> 99% cpu usage..
I think you should investigate if your problem can be solved by WaitCommEvent or WaitForSingleObject functions.
Re: Worker thread -> 99% cpu usage..
Thanks for the advice, but I looked them up and don't think my problem can be solved by those functions. At least I don't see how...
Re: Worker thread -> 99% cpu usage..
Hi There,
I think you can try SetThreadPriority, set to a low level so it should let other applications allocate processing power if needed. However with 0-5ms delay, I think it is very much likely that your processor will always be busy even when none of the more important threads allocate something since in that case the port scanner will.
Cheers
Re: Worker thread -> 99% cpu usage..
I think it's a bad idea to loop continuos inside a thread, it is bound to consume 99% of CPU.
You can try using Sleep(10) let let other threads run well.
Re: Worker thread -> 99% cpu usage..
Quote:
Originally Posted by Krishnaa
I think it's a bad idea to loop continuos inside a thread, it is bound to consume 99% of CPU.
You can try using Sleep(10) let let other threads run well.
a ::Sleep(10) would violate his constraint, wouldn't it:
Quote:
Originally Posted by Zaph-0
I have a dedicated thread in my program that always polls the parallel port in 1ms intervalls
Re: Worker thread -> 99% cpu usage..
Quote:
Originally Posted by Alin
a ::Sleep(10) would violate his constraint, wouldn't it:
Sure it will violate. I am suggesting an adjustment to requirement.
One should have saperate program to poll such tight interval, a normal program with other tasks to do can not handle pressure of having a dominating thread.