Help with 2 things: high CPU usage, GetDeviceCaps

Why does this function use 100% CPU

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void trackKeys(void *args){
    char state[255], prevState[255];
    while(1){
        for(int aa=10;aa<256;aa++){
            prevState[aa]=state[aa];
            if((char) (GetAsyncKeyState(aa)>>8)!=0){
                state[aa]=1;
            }
            else{
                state[aa]=0;
            }
            if(state[aa]==0&&prevState[aa]==1&&aa!=4&&aa!=2&&aa!=1){
                keyPresses++;
            }
        }
        Sleep(10);
    }
}


when this function doesn't?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void trackClicks(void *args){
    int leftButton, leftButtonPrev, rightButton, rightButtonPrev;
    while(1){
        leftButtonPrev = leftButton;
        rightButtonPrev = rightButton;
        if((char) (GetAsyncKeyState(1)>>8)!=0){
            leftButton=1;
        }
        else{
            leftButton=0;
        }
        if((char) (GetAsyncKeyState(2)>>8)!=0){
            rightButton=1;
        }
        else{
            rightButton=0;
        }
        if(leftButton==0&&leftButtonPrev==1){
            leftClicks++;
        }
        if(rightButton==0&&rightButtonPrev==1){
            rightClicks++;
        }
        Sleep(10);
    }
}


How can I reduce the CPU usage of the first function?

Also, what do I need to do to use the GetDeviceCaps function? I have this function but it keeps saying "undefined reference to GetDeviceCaps@8":

1
2
3
4
float getPPI(){
    HDC screen = GetDC(NULL);
    return 25.4*GetDeviceCaps(screen,HORZRES)/GetDeviceCaps(screen,HORZSIZE);
}
Last edited on
You need to link against gdi32.lib or libgdi32.a, depending on what compiler are you using. Please read MSDN documentation first.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd144877%28v=vs.85%29.aspx
Ok thanks, I got that working. Any idea about the CPU usage?
The first example should use 100% of the CPU while it is processing the for loop then it should sleep for 10 ms and start all over again. The second loop has the "Sleep()" function inside of the only loop so it is sleeping at every iteration.

The function "Sleep()" tells the scheduler to defer to another process in the queue for how ever long you pass it. During this time the thread that calls "Sleep()" is not occupying the CPU processing power.
I know... I'm asking how can I reduce the CPU usage.
Obviously you don't know or you wouldn't have been asking about it. There is several things you did wrong in your code. I'm just going to point them out but won't tell you how to fix them because it looks like global hooks.

1) Infinite loop
2) Loop nested within infinite loop
3) For loop goes out of bounds (should result in a compiler error)
4) Character arrays are old school and pointless. You're also not even doing anything with them.
5) Your functions make no use of args
6) You're assigning a potentially undefined element from one array to another.
Topic archived. No new replies allowed.