-
October 11th, 2007, 08:11 PM
#1
Program Main Loop.
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){
}
}
-
October 12th, 2007, 01:54 AM
#2
Re: Program Main Loop.
Why worry about a mainloop ?? write the program you want and if you need a loop you will know by then.
-
October 12th, 2007, 02:41 AM
#3
Re: Program Main Loop.
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.
-
October 13th, 2007, 05:13 AM
#4
Re: Program Main Loop.
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.
-
October 13th, 2007, 08:08 AM
#5
Re: Program Main Loop.
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.
-
October 15th, 2007, 01:15 AM
#6
Re: Program Main Loop.
Originally Posted by wildfire99
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.
Instead of PeekMessage() try GetMessage() so you app will be using CPU only when need it, so you can work with out Sleep(n).
-
October 15th, 2007, 07:27 AM
#7
Re: Program Main Loop.
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.
-
October 18th, 2007, 08:17 AM
#8
Re: Program Main Loop.
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
-
October 18th, 2007, 08:33 AM
#9
Re: Program Main Loop.
Well, noname00 already suggested you to use timer (SetTimer and handle WM_TIMER message). Why don't you want to implement it?
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|