Jump to content
  • Advertisement
Sign in to follow this  
Dee2

Tetris eating up all CPU

This topic is 5106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm new here, I've just gone through the "Tetris clone in an hour with C++" tutorial. I've used the SDL version and running on Linux. The game is running fine except it's using up 98% CPU time It is like it is running the same way as while(1) ; would do. How do I bring the CPU usage down, should I have some pause function or something in the main loop? Thanks.

Share this post


Link to post
Share on other sites
Advertisement
When i code windowed apps with directx in windows I usally put a Sleep(2) in the main loop to give other processes a chance to work. It usually cuts the cpu usage by half. I bet you can do the same in linux.

Share this post


Link to post
Share on other sites
Hi eriko,
Yes but I can't use sleep(2) - 2 seconds, because it has to be aware of the keyboard at all time if I want to rotate/move a brick, the sleep() function I have puts everything to sleep, you can do nothing for 2 seconds.

Share this post


Link to post
Share on other sites
Yes maybe I could lower it, but I'd rather try to fix the program itself, I found out that I could use a function called select() that will let me pause in microseconds, that helps a lot, but I'm still very open to sugesstion on how to do it better.

Share this post


Link to post
Share on other sites
I'm not sure about linux but on windows calling sleep with 0 as an argument just puts your process to sleep and wakes it up again when the scheduler determines it's time for it to run again which will lower the CPU usage, though this may mean your game misses input events (which is not good).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Use SDL_Delay(), it takes milliseconds as parameter.

Share this post


Link to post
Share on other sites
Don't bother. The OS does a way better job of managing this than you do. Why do you think it uses 98% cpu, rather than 100%?

Simple, because the OS ensures that other processes also get the cpu time they need. Basically, there is nothing to fix. At the moment, the other processes only request those 2% cpu, after which they yield the cpu again. If they didn't do that, they would take their "fair" share of the cpu. The OS keeps track of that, and honestly, don't worry about it.

You can use sleep, or something like it, if you really want the game to do nothing for a while. (Probably if you're making something multithreaded, and one thread should only run at regular intervals).

If you insert a sleep() call in your singlethreaded app, you're basically just pausing the game. Don't do this, unless you actually need it to be paused. The OS divides the cpu into time slices that are given to each process in turn. The other processes are already given permission by the OS to use a fair share of the cpu, but they have nothing to use it for. They use their 2%, and then yield, so something that *does* need the cpu can get it (In this case, that'll be your game).

If you didn't use 98% of the cpu, the remainder would just be wasted on the OS's idle loop. What do you gain by that?

Share this post


Link to post
Share on other sites
Quote:

You can use sleep, or something like it, if you really want the game to do nothing for a while. (Probably if you're making something multithreaded, and one thread should only run at regular intervals).


Hmm, you should in some way pause or delay threads you're making(not necesarily using sleep()), not sure if that goes for your single threaded game also. If you don't pause them they will try to execute as fast as possible, meaning they tell the OS they need all CPU to try to acomplish this...

I had the same problem when I wrote a multithreaded server app. When started the server was the largest CPU hog in history, pratically locking the computer up ;)
Sleep(), with a minimal delay, handled this pretty well, and the server's CPU consumption was flatned to about 0-1%, still working great...

Share this post


Link to post
Share on other sites
Quote:
Original post by Android_s
Hmm, you should in some way pause or delay threads you're making(not necesarily using sleep()), not sure if that goes for your single threaded game also. If you don't pause them they will try to execute as fast as possible, meaning they tell the OS they need all CPU to try to acomplish this...

I had the same problem when I wrote a multithreaded server app. When started the server was the largest CPU hog in history, pratically locking the computer up ;)
Sleep(), with a minimal delay, handled this pretty well, and the server's CPU consumption was flatned to about 0-1%, still working great...


There is a difference. What you had the problem with wasn't a game. No one in their right minds want a server app to use 100% cpu.

Games are *meant* to do it, to get as high framerate as possible, and to be as responsive as possible. Further, a delay of a few milliseconds in a server app isn't a problem, but a delay in a game might mean it misses your input, or other nasty effects.

The game will try to execute as fast as possible, but the OS still makes sure to distribute some cpu resources to other processes. So in effect, the game will only execute as fast as it's allowed to do. If another process suddenly requires 50% of the cpu, then it'll get that. Your framerate will suffer, but the point is that you don't have to do anything to make sure that other process gets the cpu power it needs. All that other process has to do, is stop yielding the cpu. Once that happens, it will be allowed as much cpu time as your game, assuming they have the same priority.

Really, it's not your game telling the OS it needs all the cpu, it's the other processes saying they don't need it. It's none of your business, basically. ;)

On a side note, there are usually better ways to "play nice" and ensure other processes get what they need. Instead of using sleep, which pauses your app for a specific time, use the appropriate system call to yield the cpu, to tell the OS you don't want the rest of your current timeslice. That way, you do what needs doing, and don't hog the cpu doing your own little idle-loop, waiting for your timeslice to expire.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!