Hi all,
How does one make a main loop in a c++ program? Is there any secret on it?
I've done the very simple code below and it is taking 50% of CPU Usage, so, is there any thing i should do to slow up stuf?
Regards
Code:int main(){
while(1==1){
}
}
Printable View
Hi all,
How does one make a main loop in a c++ program? Is there any secret on it?
I've done the very simple code below and it is taking 50% of CPU Usage, so, is there any thing i should do to slow up stuf?
Regards
Code:int main(){
while(1==1){
}
}
Why worry about a mainloop ?? write the program you want and if you need a loop you will know by then.
If you are using VC++ and if you want your loop to run at given time intervals, you can use the WM_TIMER message. Search MSDN for it.
Use Sleep. Sleep(10) to sleep 10 milliseconds, which will suspend your thread for 10ms so the CPU can do other things.
If you're writing a traditional windows message loop, use PeekMessage() to check for pending messages, then again take a nap with Sleep() when you're done. You can then control the temporal granularity (is that a proper term?) of your processing loops.
Of course, if you're writing a game program, it's pretty typical for it to eat up 100% of the CPU, as it is a performance-based application and should always have something to do.
WM_TIMER messages is very easy to use but is not in my experience terribly consistent under 2-3 seconds, especially as you have to get around to processing the message(s) first. If you have something that absolutely must run constantly while you're doing your usual UI stuff, then multi-threading (e.g. _beginthreadex() and all the loveliness that accompanies it) is your path.
I've never tried windows timers calling functions directly via a "TimerProc", since I try to find better ways of doing it first.
Why would you want your program to run slowly? The cpu is there to work for you.
Without more context, your question can't really be answered.
Quote:
Originally Posted by wildfire99
Instead of PeekMessage() try GetMessage() so you app will be using CPU only when need it, so you can work with out Sleep(n).
Sleeping in the main thread is not a good idea (the application will appear to be hung/non-responsive during sleep). I'd suggest to create worker thread, and do the sleeping over there :).
Regards,
Usman.
Hi felows,
I'm reading the posts and trying some advices you gave, but i'm sorry to say none of them help me.
First i tried this Sleep(...) function, witch seems to be a nice way out my problem. It stops the loop, that's true, but it doesn't help with the CPU Usage.
About using the GetMessage() loop, i'm indeed using a WIN32 api window, but i need at leat 4 turns per second for my app and if i use "while(GetMessage(...))" and all the WinProc stuff, at some point it stops getting back to my code if no action is taken at the window.
Anyway, i think GCDEF is right in his plea, so i'll give some context to my thread. I've ceated a docked window and i'm trying to display some Roll-On text on it. So i'm using the TextOut(...) function manipulating the offset as the loop turns on. Well, it is a very simple program, so i think it is absurd that it takes all that heavy processing.
Thank's you guys for wasting time with a newbie like me! I'll keep trying to solve my problem so any help is apreciated.
JorMaia
Well, noname00 already suggested you to use timer (SetTimer and handle WM_TIMER message). Why don't you want to implement it? :confused: