Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Lately, I've been thinking of ways to use multithreading to improve the speed of different parts of a game engine. What confuses me is the appropriate granularity of threads, especially when dealing with single-instruction-multiple-data (SIMD) tasks.

Let's use line-of-sight detection as an example. Each AI actor must be able to detect objects of interest around them and mark them. There are three basic ways to go about this with multithreading:

  1. Don't use threading at all.
  2. Create a thread for each actor.
  3. Create a thread for each actor-object combination.

Option 1 is obviously going to be the least efficient method. However, choosing between the next two options is more difficult. Only using one thread per actor is still running through every object in series instead of in parallel. However, are CPU's able to create and join threads in the granularity posed in Option 3 efficiently? It seems like that many calls to the OS could be really slow, and varying enormously between different hardware.

share|improve this question
up vote 5 down vote accepted

You forgot a very important variant in your list:

#4 Create a threadpool (size = number of physical CPUs) and distribute small tasks to them (e.g. each actor-object combination).

This assumes that the task at hand is CPU-bound. Hence there is nothing to win if you use more threads than there are CPUs. Using more threads only adds the overhead of thread scheduling. This also assumes, that you can split / join your tasks without to much locking overhead.

share|improve this answer
Makes sense, seeing as pretty much all these super-granular tasks are purely calculation. – jmegaffin Dec 11 '12 at 20:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.