When to use multi-threading and when to not

Started by
10 comments, last by Hodgman 11 years, 8 months ago
Add threads only for performance reasons.

  • When you do a simple application, don't use them.
  • When you use heavy input and output operations or do other long tasks. Use predefined threads. Like two threads, first for the main loop and the second for asynchronous operations.
  • Or you want an scalable and fast application (mostly engines), then you should use task based threads (like TBB). But they are only useful for computers with more then two cores.

This above is a very weak classification. The need of multi-threading really depends on your design and your costumers.

Regards
Ömercan
Reading, Reading, Reading... why do you read not more?
I have a blog: omercan1993.wordpress.com look there for more content
And I also do some art: omercan1993.deviantart.com
And whats about the Pear3DEngine? Never heard of it? Go and look!
Yeah, and currently I do this: SimuWorld

PS: Please look at this poll on my blog about scripting languages: A opinion poll about scripting language
Advertisement
At an OS level, the point of threads is to allow the computer to do two things at once, e.g. keep a GUI responsive while processing a large file.

However, this is not the case with games. Games are realtime applications, which means the execution times of our functions always has to have an upper bound -- i.e. the Update function in our main loop is written in such a way that it only ever takes 33ms, etc... So, we don't need threads for this purpose, and using threads for this purpose is actually harmful, as it increase the unpredictability of our "main" thread(s) (if a non-realtime background thread steals our CPU time, maybe update will take longer than 33ms to run!).

So, the purpose of threads in realtime games is instead only to take advantage of multi-core CPUs. If you have some task that is so computationally expensive that it cannot complete fast enough, then you can optimise that task by splitting it over several CPU cores, which is an advanced optimization topic.

If your game runs at 30Hz on a single CPU core, then you do not need to bother with multi-threading.

This topic is closed to new replies.

Advertisement