Threading in Games

Jan 23, 2011 at 4:21pm
Hi there,

Lately I've been investigating the possible uses of threading in computer game, of which I am aware that there are quite a few that are quite commonly utilized.

I have no problems making use of threads, but I was wondering about what possible uses you can think of for them in computer games. I am working on a sidescrolling, networked multiplayer game, so threading applicable to those particular game attributes would be the most helpful. What ways do game programmers make use of threads in those particular contexts and game programming in general?

I have thought of a few uses of threads so far, although I have not implemented them as I know very little about the practical applications of threading. I would appreciate advisory commentary and recommendations based on the following applications of threading:

* Video rendering (OpenGL) independent of the game logic loop allowing uncapped frame rates regardless of the rate at which the game loop iterates. I understand that OpenGL is not threading friendly, but I figure that this won't be a problem as long as OpenGL operations are confined to a single thread. I've heard something about deltas? I'm not exactly sure what they are, but I think they have something to do with predicting the positions and states of things regardless of the game's frame rate?

* Audio handling and playback, especially with regards to synthesizing sound effects and procedural and dynamic generation of music. I'm very interested in real-time, procedurally generated content in general. I'm not sure what audio library to use in my game yet, but I'm sure that threading is important to audio in general.

* Window message management and handling. I've noticed one a few occasions that if my program is performing a complex function that could takes several seconds to perform, my operating system (Windows 7) will regard the application as not responding and crash it. Perhaps using a thread to receive and handle window and system messages (ideally at specific intervals) could help to solve problems like this.

* Artificial intelligence, perhaps?

Those are all of the uses I'd like to make of threads at the moment. However, I don't really know where to begin. Does anyone have any advice they could offer me on how to pursue this? Links to articles/tutorials would be good too.

Generally speaking, any and all thread related discussion here is a good thing!

Thanks!
- Sig

* Networking. I haven't started on my network code yet, but I'm sure having at least one thread to receive and interpret network data is essential.
Jan 23, 2011 at 4:50pm
I have gone out of Game Development myself for a while to learn to apply just these sort of things before I go on and try it again..

Threads seem to be useful in a variety of ways: resource loading while not halting game progression, opening connections while not halting game progression, playing audio while not halting the game and others. Generally anything that you want to run simultaneously with the actual game (note though, that SFML already handles some of these in new threads).
Jan 23, 2011 at 7:21pm
Your biggest gain by multi threading a game on a modern PC is by using loading blocks. If you plan it just right then your player should almost never run into a loading block other then starting the game up. For a side scroller it would be kind of easy, you would want to load\prerender the next stage into a vector as the player progresses through the current one. Then when he hits the point to progress you move the pointer that was feeding your game loop to the next vector and repurpose the old vector to load the next stage. Do the same for cut scenes or whatever else.

A distant second place might be saving progress, if this could be off loaded to another thread, especially during auto saves, then the player will be free to continue playing unobstructed. The downfall here is that most platforms do not like orphan threads so you have to be familair enough with the environment to make sure that the parent thread does not die before it's child threads do.

Threading the Audio is more a question of asthetic effect. Multi threading this won't boost the speed of your game much but it would allow you to play sounds parallel with eachother.

When you're writing for a Windows box you should always assume that the OS will want to stick it's paws into your game, weither it has a reason to or not. So yeah a WndProc CALLBACK function to handle interupts would be good. While you're at it you should use this function to detect if the window is minimized\has focus etc. so that you know to raise a flag and pause the game, this will prevent the player from dying when say Adobe feels like it wants to do an update (has it been 5 mins already).

AI is over rated in video games. A switch case statement like the ones you see used in the "Hammer" game engine is really all you need.

Also I agree with Kyon %100 SFML is the way to go with multi-threading, even if it's the only function you use from the library the tutorial is direct and the results are fantastic!
Last edited on Jan 23, 2011 at 7:23pm
Topic archived. No new replies allowed.