Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Sign up now

How much CPU usage should just an empty game loop have

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
23 replies to this topic

#21 Erik Rufelt   Members   


Posted 26 October 2012 - 02:57 AM

these are essentially the same questions, posed at the start of both threads, but what i took away from my thread was that the cpu is just another resource, use it to it's fullest, and here, i'm taking away that the game should use as little cpu resource as possible, to avoid unnecessary damage to user hardware.

I'd say the solution you posted in your thread is correct, on desktop PCs, don't care when your app is active, sleep when it's in the background.
The problem usually takes care of itself with vsync. If the user chooses to disable vsync, then they want as high a framerate as possible, which is obviously achieved by never waiting, and with vsync the OS will hopefully not use more CPU than it needs to.
Some games have a separate "limit framerate" option that can be enabled without vsync. If you implement that, and it is active, then you might want to do something other than just spinning on your timer function, just to play nice and allow users that want to decrease CPU usage for whatever reason to do so.

#22 L. Spiro   Members   


Posted 26 October 2012 - 03:16 AM

1. If your fans are spinning up when you run a while(true); then you have a serious problem. That is not to say that you should run such a loop, but the idea that 100% CPU usage is going to overheat and melt your machine is ludicrous. That may have been true a long time ago, on hardware from the 1980s, but if your machine is melting down on a while(true); loop you've got a lot bigger issues than said loop.

I agree that just using 100% of the CPU is unlikely to cause damage, and mentioned so in my post.
But I do have recent experiences with overheating on graphics cards (much more common). One of my laptops is only a few years old and has been permanently damaged from overheating to the point where I can’t even turn it on anymore.
However, just dong a while ( true ) loop is not possibly going to cause such a case as you correctly point out.

2. Windows (and in fact pretty much every operating system you will find on PC class hardware) is a pre-emptive multitasking operating system. This means every single thread is allocated a slice of the CPU's time and when that slice of time is up it is preempted and another thread scheduled. All processes, in Windows, will get their own slice of time and a chance to do their work. If it were otherwise then your game would be unable to render, as all of the actual work happens during the driver/kernel's time slice. You can resource starve threads, but that's an issue for your own process and not for other processes that are running concurrently, and if you screw around with thread priorities you can increase your timeslice, but usually only to your own detriment (if you starve the kernel's threads... most of the work you schedule just doesn't get done).

I hope I didn’t come off as implying otherwise.
I just assumed the original poster is doing something strange because I don’t think a normal empty game loop should otherwise be taking 60% of the CPU.
It turns out he was on Android, which explains that.

slicer4ever, in general a game should be trying to use all resources available.
But it should only be able to reach that point when it is actually a full game, not just an empty loop.
I still don’t see a problem between your replies.
You are on Windows, and while there are things to consider about power usage etc., it is not hugely important for you to consider them. Many games don’t. I have lost hardware because of it, but it is up to you to take those things into consideration on your own will.
I don’t think it is something that needs to be brought up every time this question is asked, so you don’t need to mind so much that it wasn’t mentioned in your post.
You got the correct reply for your needs.
It was just fortuitous that this poster also got the correct reply for his needs, which include battery conservation.

L. Spiro

Edited by L. Spiro, 26 October 2012 - 05:32 AM.

#23 mhagain   Members   


Posted 26 October 2012 - 03:46 AM

The key point here, I think, is that under some circumstances waiting sure is appropriate, but Sleep calls are not a good way to achieve that waiting.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

#24 freeworld   Members   


Posted 26 October 2012 - 09:57 AM

@lspiro my appologies for my rudeness. The OP never stated his target platform let alone much else. The point i was trying to get across is hes worrying way too much about the little things. Which stinks of someone that does 99% theorizing and 1% coding. I was trying to get across the point that he should code his app and not worry about these things yet. As he gets experience with common headaches hell learn how better to identify them and avoid em.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.