• my program use 100 % of cpu usage all the time.
    18 replies, posted
Hi I'm using the win32 api and I have a problem with my program. The progam use 100 % of the cpu usage all the time, even when it's not "marked/active" So how can I fix this? And when I use this piece of code: [code] case WM_MOUSEMOVE: { winMousePos.x = LOWORD(lParam) ; winMousePos.y = HIWORD(lParam); break; } [/code] all the other programs is running very slow, like steam, the menus is lagging :S You guys know how I can fix this problem? Sorry for my awesome english.
sleep(0) to give back the rest of your time slice to the OS
Ok, but I seriously don't know where in the callback func I should use it. :(
[QUOTE=likesoursugar;19637761]Ok, but I seriously don't know where in the callback func I should use it. :([/QUOTE] At the end of your programs main loop.
[QUOTE=r4nk_;19637643]sleep(0) to give back the rest of your time slice to the OS[/QUOTE] More like Sleep(10); Now my program use around 30 % :O Oho. well when I minimize the window the usage is 0 %, that's very good. but when the window is focused the cpu usage is 100 % ( when I use Sleep(1); )
-snip-
[QUOTE=likesoursugar;19637874]More like Sleep(10); Now my program use around 30 % :O Oho. well when I minimize the window the usage is 0 %, that's very good. but when the window is focused the cpu usage is 100 % ( when I use Sleep(1); )[/QUOTE] Just what are you doing in your program? Sleep(0) just makes the current thread yield immediately, which guarantees the program to use virtually no processing power [I]unless[/I] you're actually doing a lot in the loop, every frame. In a way, it gives away whatever processing power you can spare. I can imagine you either put the call to Sleep in the wrong place, or you're doing something very wrong in your message loop (since it goes to 0 when you minimize it).
[QUOTE=jA_cOp;19638072]Just what are you doing in your program? Sleep(0) just makes the current thread yield immediately, which guarantees the program to use virtually no processing power [I]unless[/I] you're actually doing a lot in the loop, every frame. In a way, it gives away whatever processing power you can spare. I can imagine you either put the call to Sleep in the wrong place, or you're doing something very wrong in your message loop (since it goes to 0 when you minimize it).[/QUOTE] I'm using the win32 api and opengl for my rendering. That's probably why it wont work. [code] if (!CreateGLWindow("Vector training 1",SCREEN_WIDTH,SCREEN_HEIGHT,16,fullscreen)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received? { done=TRUE; // ESC or DrawGLScene Signalled A Quit } else // Not Time To Quit, Update Screen { SwapBuffers(hDC); // Swap Buffers (Double Buffering) } if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // Kill Our Current Window fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode // Recreate Our OpenGL Window if (!CreateGLWindow("Vector training 1",SCREEN_WIDTH,SCREEN_HEIGHT,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } if (keys[VK_UP]) // Is F1 Being Pressed? { keys[VK_UP]=FALSE; gun_pos.y += 1; } if (keys[VK_DOWN]) // Is F1 Being Pressed? { keys[VK_DOWN]=FALSE; gun_pos.y -= 1; } if (keys[VK_LEFT]) // Is F1 Being Pressed? { keys[VK_LEFT]=FALSE; gun_pos.x -= 1; } if (keys[VK_RIGHT]) // Is F1 Being Pressed? { keys[VK_RIGHT]=FALSE; gun_pos.x += 1; } Sleep(1); } } // Shutdown KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program } [/code]
[cpp] if (!CreateGLWindow("Vector training 1",SCREEN_WIDTH,SCREEN_HEIGHT,16,fullscreen)) { return 0; // Quit If Window Was Not Created } while(!done) // Loop That Runs While done=FALSE { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) // Is There A Message Waiting? { if (msg.message==WM_QUIT) // Have We Received A Quit Message? { done=TRUE; // If So done=TRUE } else // If Not, Deal With Window Messages { TranslateMessage(&msg); // Translate The Message DispatchMessage(&msg); // Dispatch The Message } } else // If There Are No Messages { // Draw The Scene. Watch For ESC Key And Quit Messages From DrawGLScene() if ((active && !DrawGLScene()) || keys[VK_ESCAPE]) // Active? Was There A Quit Received? { done=TRUE; // ESC or DrawGLScene Signalled A Quit } else // Not Time To Quit, Update Screen { SwapBuffers(hDC); // Swap Buffers (Double Buffering) } if (keys[VK_F1]) // Is F1 Being Pressed? { keys[VK_F1]=FALSE; // If So Make Key FALSE KillGLWindow(); // Kill Our Current Window fullscreen=!fullscreen; // Toggle Fullscreen / Windowed Mode // Recreate Our OpenGL Window if (!CreateGLWindow("Vector training 1",SCREEN_WIDTH,SCREEN_HEIGHT,16,fullscreen)) { return 0; // Quit If Window Was Not Created } } if (keys[VK_UP]) // Is F1 Being Pressed? { keys[VK_UP]=FALSE; gun_pos.y += 1; } if (keys[VK_DOWN]) // Is F1 Being Pressed? { keys[VK_DOWN]=FALSE; gun_pos.y -= 1; } if (keys[VK_LEFT]) // Is F1 Being Pressed? { keys[VK_LEFT]=FALSE; gun_pos.x -= 1; } if (keys[VK_RIGHT]) // Is F1 Being Pressed? { keys[VK_RIGHT]=FALSE; gun_pos.x += 1; } } Sleep(1); /*moved this out of the if-statement and into the loop*/ } // Shutdown KillGLWindow(); // Kill The Window return (msg.wParam); // Exit The Program } [/cpp] See the comment on Sleep.
[QUOTE=jA_cOp;19638451] See the comment on Sleep.[/QUOTE] Ehee. I used one of my previous file and I just deleted some code there and forgot to remove the comment :P Happens all the time for me.
[QUOTE=likesoursugar;19638519]Ehee. I used one of my previous file and I just deleted some code there and forgot to remove the comment :P Happens all the time for me.[/QUOTE] I meant, look at the comment on the Sleep call. I added that. I moved Sleep out of the if-statement and into the top scope of the main loop (while(!done){...}). That way it will yield once per frame, whether you receive a message that frame or not. It will fix your CPU problem.
[QUOTE=jA_cOp;19638554]I meant, look at the comment on the Sleep call. I added that.[/QUOTE] Ok im dumb, lol [QUOTE] It will fix your CPU problem.[/QUOTE] I'm afraid it didn't fix the cpu problem :( I've tried that. [editline]05:45AM[/editline]
Shouldn't use Sleep at all in my opinion. If it's using up your CPU while minimized then you should probably use [URL=http://msdn.microsoft.com/en-us/library/ms644956%28VS.85%29.aspx]WaitMessage[/URL] when the app is minimized (WM_SIZE), and keep waiting until you get a message that it's been restored or maximized. If you still want to limit the CPU usage while rendering, you should limit the FPS and when there's idle time, then Sleep. Vertical sync might do that automatically, I don't know. [editline]12:21PM[/editline] Rather than merely using Sleep, which is stupid, this will guarantee that the application has a maximum frame rate on all computers and should run smoothly rather than stuttering on slow computers and will give time to other threads when there's time.
[QUOTE=Spoco;19641514]Shouldn't use Sleep at all in my opinion. If it's using up your CPU while minimized then you should probably use [URL=http://msdn.microsoft.com/en-us/library/ms644956%28VS.85%29.aspx]WaitMessage[/URL] when the app is minimized (WM_SIZE), and keep waiting until you get a message that it's been restored or maximized. If you still want to limit the CPU usage while rendering, you should limit the FPS and when there's idle time, then Sleep. Vertical sync might do that automatically, I don't know. [editline]12:21PM[/editline] Rather than merely using Sleep, which is stupid, this will guarantee that the application has a maximum frame rate on all computers and should run smoothly rather than stuttering on slow computers and will give time to other threads when there's time.[/QUOTE] Sleep(0); will have a negligible performance impact, it doesn't cause a pause it just gives back the remainder of the timeslice it was allocated by the scheduler. It won't cause slowdown on slower computers because if it doesn't complete it's revolution of the loop then it won't give time back, it will take all of the timeslice it needs and then give back to the OS what is left over. The only problem is if the loop takes only very slightly longer than the timeslice, in which case you are basically giving away an entire timeslice the next time the scheduler gives you one. The problem you described is not actually a problem with Sleep(0), that only occurs when you set it to like 33 for 30fps.
Don't add sleep(), figure out WHY your using so much CPU. Looks like you're endlessly polling for messages (which may be expensive), and it looks like you do some memory copies/rendering (??) every single loop. Step back for a moment and re-design your code. Can you put the 'peek message' code else where? Can you avoid the constant rendering?
[QUOTE=streeter;20022555]Don't add sleep(), figure out WHY your using so much CPU.[/QUOTE] It's using so much CPU time [I]because[/I] he's not giving up any of his time slice. He's not calling any blocking system functions, and he's not giving up any time manually. A program will always use as much CPU time as it can unless the program tells the OS otherwise. Nice two-week bump, by the way...
How do you tell the OS otherwise then? I have this problem also? Is Sleep() the correct way to do it or is it just a hacky method?
[url]http://msdn.microsoft.com/en-us/library/ms686298%28VS.85%29.aspx[/url] Says it right there. How else would you do it, anyway?
Wait for the WM_ACTIVATE message before beginning, then pause on WM_DEACTIVATE.
Sorry, you need to Log In to post a reply to this thread.