views:

896

answers:

11

I don't know how commercial games work inside very much, but the open source games I have come across don't seem to be massively into threading. Same goes for most other desktop applications, normally two or three threads seem to be used (eg program logic and GUI updates).

Why don't games have many threads? Eg separate threads for physics, sound, graphics, AI etc?

+15  A: 

The main reason is that, as elegant as it sounds, using multiple threads in a program as complicated as a 3D game is really, really, really difficult. Also, before the fairly recent introduction of low cost multi-core systems, using multiple threads did not offer much of a performance incentive.

William Brendel
+20  A: 

I don't know about the games that you have played, but most games run the sound on a separate thread. Networking code, at least the socket listeners run on a separate thread.

However, the rest of the game engine generally runs in a single thread. There are reasons for this. For example, most processing in a game runs a single chain of dependencies. Graphics depend on state of physics engine as does the artificial intelligence. Designing for multiple threads means that you have to have frame latency between the various subsystems for concurrency. You get quicker response time and snappier game play if these subsystems are computed linearly each frame. The part of the game that benefits the most from parallelization is of course the rendering subsystem which is offloaded to highly parallelized graphics accelerator cards.

postfuturist
A: 

There are many issues with race conditions and data locking when using lots of threads. Since the different parts of games are fairly reliant on each other it doesn't make much sense to do all the extra engineering required to use loads of threads.

Gavin Schulz
+1  A: 

I was about to post the same thing as William, but I'd like to expand on it a little bit. It's very hard to write optimal code for the future. Given the choice between writing something that will scale to hardware you don't have vs. writing something that will work on hardware you do have, most people will chose to do the latter. Since the single-core paradigm has been with us for so long, most code that has been written (especially for games where there is extreme pressure to get it out the door) isn't that future proof.

x86 has been very kind to game programmers, since we haven't had to think about the ramifications of less forgiving hardware platforms.

MSN
A: 

It's very difficult to use threads without problems, and most GUI APIs are based on event driven coding anyway. Threads mandate the use of locking mechanisms which add delay to the code, and often that delay is unpredictable depending on who is currently holding the lock.

It seems sensible to me to have a single (or perhaps very few) threads handling things in an event driven way rather than hundreds of threads all causing strange and unrepeatable bugs.

Adam Hawes
+1  A: 

Other than the technical challenges of programming for multiple cores, commercial games have to run well on low end systems w/o multiple cores to make money.

Now that multi-core processors have been out for a while and the major game consoles have multiple cores it's only a matter of time before dual core shows up on the minimum system requirements list for PC games.

Here's a link to an interview with Orion Granatir from Intel where he's talking about getting game developers to take advantage of multi-threading.

Seth Reno
A: 

The fact that everybody here is correctly claiming that multithreading is hard is very sad. We desperately need to make concurrency systems easy.

Personally I think we are going to need a paradigm shift and new tools.

Pyrolistical
unfortunately, that's the way it is.Tools won't make threads easier (we already have some, eg openmp or TBB). The problem is simply hard. It is possible we will get better tools, but they will appear at the cost of concurrent performance in order to make apps work correctly.
gbjbaanb
paradigm shift, then tools ;)
Pyrolistical
+13  A: 

You need to think, what are the actual benefits of threads? Remember that on a single core machine, threads don't actually allow concurrent execution, just the impression of it. Behind the scenes, the CPU is context-switching between the different threads, doing a little work on each every time. Therefore, if I have several tasks that involve no waiting, running them concurrently (on a single core) will be no quicker than running them linearly. In fact, it will be slower, due to the added overhead of the frequent context-switching.

If that is the case then, why ever use threads on a single core machine? Well firstly, because sometimes tasks can involve long periods of waiting on some external resource, such as a disk or other hardware device, to become available. Whilst the task in a waiting stage, threading allows other tasks to continue, thus using the CPU's time more efficiency.

Secondly, tasks may have a deadline of some sort in which to complete, particularly if they are responding to an event. The classic example is the user interface of an application. The computer should respond to user action events as quickly as possible, even if it is busy performing some other long running task, otherwise the user will be become agitated and may believe the application has crashed. Threading allows this to happen.

As for games, I am not a games programmer, but my understanding of the situation is this: 3D games create a programmatic model of the game world; players, enemies, items, terrain, etc. This game world is updated in discrete steps, based on the amount of time that has elapsed since the previous update. So, if 1ms has passed since the last time round the game loop, the position of an object is updated by using its velocity and the elapsed time to determine the delta (obviously the physics is a bit more complicated than that, but you get the idea). Other factors such as AI and input keys may also contribute to the update. When everything is finished, the updated game world is rendered as a new frame and the process begins again. This process usually occurs many times per second.

When we think about the game loop in this way, we can see that the engine is in fact achieving a very similar goal to that of threading. It has a number of long running tasks (updating the world's physics, handling user input, etc), and it gives the impression that they are happening concurrently by breaking them down into small pieces of work and interleaving these pieces, but instead of relying on the CPU or operating system to manage the time spent on each, it is doing it itself. This means it can keep all the different tasks properly synchronized, and avoid the complexities that come with real threading: locks, pre-emption, re-entrant code, etc. There is no performance implication to this approach either, because as we said a single core machine can only really execute code linearly anyway.

Things change when have a multi-core system. Now, tasks can be running genuinely concurrently and there may indeed be a benefit to using threading to handle different parts of the game world updates, so long as we can manage to synchronise the results to render consistent frames. We would expect therefore, that with the advent of multi-core systems, games engine developers would be working on this. And so it turns out, they are. Valve, the makers of Half Life, have recently introduced multi-processor support into their Source Engine, and I imagine many other engine developers are following suit.

Well, that turned out a little longer than I expected. I'm not a threading or games expert, but I hope I haven't made any especially glaring errors. If I have I'm sure people will correct me :)

Jon Rimmer
Actually, even on a single core machine threads can have performance gain. Some machines switch will swap the running thread due to memory stalls. If I remember correctly, Intel's HyperThreading does something similar.
Asaf R
+1 for detailed answer
Jens Roland
+3  A: 

I would ask "why?" first. What improvements do you expect from "threading" on those games. Aren't they fast enough already?

If you see a game that is slow, you still don't know if multithreading would help. For instance if the game is slow because it loads too many polygons to your video card more than it can handle, multithreading won't help you there. You need other kinds of optimization.

Multithreading can actually make things slower if there are more CPU intensive threads than number of cores because of switch overhead between threads. In some cases "controlled scheduling", or "cooperative multitasking" can perform even better than simple preemptive scheduling. That's why there are "fibers" and ad-hoc scheduling mechanisms.

To summarize: don't fix anything not broken. and if it's broken make sure you use the right tools :)

ssg
Nope. It's a reasonable question. Games are *never* fast enough unless you're playing Tetris. Which single industry do you think has fueled the GPU explosion, with GPUs now rivalling CPUs in high performance computing? (see nvidia.com/object/personal_supercomputing.html/… for example).
Justicle
+9  A: 

Many games these days are using "task" or "job" systems for parallel processing. That is, the game spawns a fixed number of worker threads which are used for multiple tasks. Work is divided up into small pieces and queued, then sent to be processed by the worker threads as they become available.

This is becoming especially common on consoles. The PS3 is based on Cell architecture so you need to use parallel processing to get the best performance out of the system. The Xbox 360 can emulate a task/job setup that was designed for PS3 as it has multiple cores. You would probably find for most games that a lot of the system design is shared among the 360, PS3, and PC codebases, so PC most likely uses the same sort of tactic.

While it is hard to write threadsafe code, as many of the other answers indicate, I think there are a few other reasons for the things you're seeing:

  • First, many open source games are a few years old. Especially with this generation of consoles parallel programming is becoming popular and even necessary as mentioned above.
  • Second, very few open source projects seem concerned about getting the highest possible performance. As John Carmack pointed out to the Utah GLX project, highly optimized code is often harder to maintain than unoptimized code, so the latter would generally be preferred in open source contexts.
  • Third, I wouldn't take a small number of threads created by a game to mean that it's not using parallel jobs well.
Dan Olson
A: 

Threads are dead, baby.

Realistically, in game development, threads don't scale beyond offloading very dedicated tasks like networking and loading. Job-systems seem to be the only way forward, given 8 CPU systems are becoming more commonplace even on PCs. And you can pretty much guarantee that upcoming super-multicore systems like Intel's Larrabee will be job-system based.

This has been a somewhat painful realization on Playstation3 and XBOX360 projects, and it seems now even Apple has jumped on board with their "revolutionary" Grand Central Dispatch system in Snow Leopard.

Threads have their place, but the naive promise of "put everything in a thread and it will all run faster" simply doesn't work in practice.

Justicle