Computing.Net > Forums > Programming > C++ code to limit CPU use?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ code to limit CPU use?

Reply to Message Icon

Name: Brandon W.
Date: November 18, 2005 at 09:00:23 Pacific
OS: Windows XP Pro SP2
CPU/Ram: PIII 1GHz 256M
Comment:

I've written a simple program to run in background, but it uses 100% of the CPU kernel. Is there a way to limit its use to 5% or less by modifying the code? I'm using Microsoft VC++ 6 Enterprise.

I already know about the Task Manager method of prioritizing. That's not what I'm after.



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: November 18, 2005 at 12:59:34 Pacific
Reply:

If you are running in a tight loop, you should either have the app sleep or allow for Windows Messages to be processed. This is the only way to free up the cpu cycles without changing it's priority.

Chi

They mostly come at night...mostly.


0

Response Number 2
Name: Brandon W.
Date: November 18, 2005 at 13:06:15 Pacific
Reply:


So there's no way to calculate the current CPU use, then increase a sleep timer inrementally to lower the resource use? I need it to loop as fast as possible, but without a significant cost in other application performance.

When I modify the source to run in console, it barely uses any CPU. Perhaps it's the Windows API?


0

Response Number 3
Name: Dr. Nick
Date: November 19, 2005 at 19:19:38 Pacific
Reply:

It might depend on what you're doing.

I have a program I wrote that runs in the background. The main part of the program is a small while(1) loop that breaks when the app receives the WM_QUIT message. To prevent it from eating the CPU I put Sleep(1000); at the end of the loop.

Waiting a full second isn't really necessary if you want it to run faster. The smallest value you can use with Sleep() is 1 millisecond and on my 2GHz machine that still keeps the CPU usage at about 0%.

Does putting Sleep(1); inside your loop let it run fast enough?



0

Response Number 4
Name: Brandon W.
Date: November 29, 2005 at 09:09:18 Pacific
Reply:

It uses 0% CPU, but it runs a bit too slow.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


java codes help wxToolBox - new wxWidgets...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ code to limit CPU use?

compile linux snmp c code to win2k www.computing.net/answers/programming/compile-linux-snmp-c-code-to-win2k/3844.html

c++ code convert .bmp to .JPEG file www.computing.net/answers/programming/c-code-convert-bmp-to-jpeg-file/4383.html

!!! help to Debug C code programmin www.computing.net/answers/programming/-help-to-debug-c-code-programmin/7888.html