How much CPU usage should just an empty game loop have

Started by
22 comments, last by freeworld 11 years, 5 months ago

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.
Advertisement

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

@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.

This topic is closed to new replies.

Advertisement