CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2004
    Posts
    62

    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 ?

  2. #2
    Join Date
    May 2006
    Posts
    327

    Re: Worker thread -> 99% cpu usage..

    I think you should investigate if your problem can be solved by WaitCommEvent or WaitForSingleObject functions.

  3. #3
    Join Date
    Apr 2004
    Posts
    62

    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...

  4. #4
    Join Date
    May 2002
    Location
    Germany
    Posts
    451

    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

  5. #5
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    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.
    Regards,
    Ramkrishna Pawar

  6. #6
    Join Date
    Feb 2002
    Posts
    3,788

    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

  7. #7
    Join Date
    Aug 1999
    Location
    <Classified>
    Posts
    6,882

    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.
    Regards,
    Ramkrishna Pawar

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured