Click here to Skip to main content
Click here to Skip to main content

CPU Load Control

, 22 Feb 2007
Rate this:
Please Sign up or sign in to vote.
Demonstration to control the amount of CPU load used by your computer
Screenshot - img.png


This is a basic MFC program that demonstrates how to retrieve the current CPU load percentage and set it using a high priority thread control loop. I created this project simply because I needed to test another fairly time/buffer critical application and wanted to simulate heavy amounts of CPU load and/or a slower computer. By using this app, you can successfully "steal" away certain amounts of processor instructions to emulate slower or more heavily loaded machines.

It only fully works on single core systems. I personally have a dual core machine, and therefore the one thread that runs in this program can never utilize more than 50% of the "Total Processor Load." I tried launching multiple threads that would hopefully use both cores, but it didn't work - I'm guessing you would have to somehow instruct a 2nd thread to use your second core. Anyway, it wasn't worth it for me to implement, so if anyone wants to add full support for multi-core machines, please post how it's done to others.

Included is a project built using VS 2005. The code is very simple and could be easily added to another project. To retrieve the CPU load percentage, you must include the dependency pdh.lib in your Project->Linker settings.


  • 22nd February, 2007: Initial post


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author


United States United States
No Biography provided

Comments and Discussions

Questiongood Pinmemberbleachli9-Nov-11 5:13 
GeneralXP is showing two processors PinmemberLokanatha Reddy15-May-07 23:38 
GeneralCPU Load Ported on Visual C++ 6.0 Pinmembercristitomi15-Mar-07 23:54 
GeneralNot work in one core PC with Windows2000 Professional PinmemberAleksMain27-Feb-07 2:24 
GeneralRe: Not work in one core PC with Windows2000 Professional Pinmemberjkhax0r2-Apr-07 6:38 
GeneralMulticore PinmemberJoergen Sigvardsson22-Feb-07 11:00 
GeneralRe: Multicore PinmemberVasudevan Deepak Kumar22-Feb-07 18:08 
QuestionRe: Multicore Pinmemberjkhax0r26-Feb-07 5:16 
I tried setting the thread affinity with no luck. Based on the documentation, I seem to have set everything up correctly - the following code detects my two processors, and starts two threads setting up each to run on a different processor, but the load still maxes out at 50%. I setup the load thread to run a while(1){} loop just to make sure and still can't go above 50%. The thread affinity on the process and the thread prior to calling SetThreadAffinity() was set to 0x03, which should have meant they would run on both processors anyway. Maybe I just missed something in the documentation... any suggestions from anyone with more experience w/ multicores?

//Update to get number of processors
DWORD procmask = 0;
DWORD systemmask = 0;
//Get the current processor and system affinity to determine what processors are avaialble
//and which this process can use
HANDLE proc = GetCurrentProcess();
GetProcessAffinityMask(proc, &procmask, &systemmask);
//Count the number of bits set to get the total number of processors on the system
DWORD temp = systemmask;
int numprocs = 0;
while (temp)
if (temp & 0x00000001) //increment count for each bit (processor) set
temp >>= 1;
if (systemmask != procmask)
SetProcessAffinityMask(proc, systemmask);

m_desiredLoad = 0;
HANDLE thread;
DWORD threadaffinity = 0x00000001; //first thread runs on proc1
//now create a thread for each processor and set it to run on a unique proc
for (int i = 0; i < numprocs; ++i)
thread = CreateThread(NULL, 0, &ThreadProc, this, 0, NULL);
DWORD res = SetThreadAffinityMask(thread, threadaffinity);
threadaffinity <<= 1;

AnswerRe: Multicore PinmemberJoergen Sigvardsson26-Feb-07 7:29 
GeneralRe: Multicore PinmemberGreat-er26-Feb-07 23:54 
GeneralRe: Multicore PinmemberJoergen Sigvardsson27-Feb-07 2:36 
GeneralRe: Multicore [modified] PinmemberGreat-er27-Feb-07 8:56 
GeneralRe: Multicore Pinmemberjkhax0r28-Feb-07 16:46 
QuestionMaybe this would work? PinmemberIvo Beltchev22-Feb-07 10:59 
AnswerRe: Maybe this would work? PinmemberVasudevan Deepak Kumar22-Feb-07 18:09 
GeneralRe: Maybe this would work? Pinmemberdongbaek19-Jul-09 22:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 22 Feb 2007
Article Copyright 2007 by jkhax0r
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid