When to use multi-threading and when to not
#2 Members - Reputation: 1210
Posted 24 August 2012 - 02:09 PM
Using separate threads for certain game program components such as audio streaming and networking come quite naturally and may be added when the program requires it.
Otherwise separating the workload on multiple threads correctly isn't always that simple. It requires you to make design decisions in the early phase and since you aren't familiar with the subject, I don't see it to be a simple task. Adding multiple threads afterwards may turn out to be quite difficult too.
Answering your question isn't easy. Multiple threads can be used in many applications, but I wouldn't rush into before defining the problem more precisely.
In my personal engine project I'm using multiple threads in audio streaming and networking. I'd like to implement threads elsewhere too such as for physics, but when ever I check the CPU load (which stays at few %) I realize that I don't need it yet.
Of course in order to squeeze all the performance of the current and future CPUs, using multiple threads is necessary.
Cheers!
Edited by kauna, 24 August 2012 - 02:10 PM.
#3 Members - Reputation: 209
Posted 24 August 2012 - 02:15 PM
#4 Members - Reputation: 1426
Posted 24 August 2012 - 02:18 PM
- avoiding UI hangs for I/O operations like reading/writing to disk, or networking (even so, asynchronous APIs may hide the underlying multi-threaded nature of these. For instance the networking and audio functionality built in to XNA is multithreaded under the covers, but you access it all from a single thread).
- spread workload over multiple cores to help give an incremental improvement with performance. But it should be a last resort. You have the potential for much greater performance gains with algorithmic improvements than you do with multi-threading (which requires a lot of very careful consideration).
#5 Members - Reputation: 392
Posted 24 August 2012 - 04:21 PM
Often the APIs you're using will use multiple threads internally, such as the FMOD sound system. There's no need to do it yourself.
If disk I/O is a problem, it's easier to use the asynchronous I/O provided by the operating system rather than multiple threads. If data processing is the bottleneck, it's probably better to process your data files offline and have the game read simpler binary structures from disk.
Hope that helps.
[Edit] The earlier thread (no pun intended) is here.
Edited by Telios, 24 August 2012 - 04:23 PM.
#8 Members - Reputation: 240
Posted 24 August 2012 - 07:57 PM
If the problem does not involve more than one task, using multiple threads should be avoided.
In the case of the game, you have three tasks (input, processing, and output), and Operating System limitations often require input and [visual] output to be done on the same thread. Processing (ai and physics) and audio output are deserving of their own threads.
Edited by nfries88, 24 August 2012 - 07:58 PM.
#9 Members - Reputation: 1338
Posted 24 August 2012 - 10:45 PM
The explicit purpose of multi-threading is to allow simultaneous execution of multiple tasks.
If the problem does not involve more than one task, using multiple threads should be avoided.
In the case of the game, you have three tasks (input, processing, and output), and Operating System limitations often require input and [visual] output to be done on the same thread. Processing (ai and physics) and audio output are deserving of their own threads.
I would have to strongly disagree with your definition of tasks in a game. Trying to put physics, ai and rendering in separate threads is a mess of endless dependencies and I was under the impression this misguided approach has been abandoned by most game developers years ago.
Tasks are way smaller than that. Updating ONE enemy AI (or rather #enemies / #cores) is a task,if those updates are independent of each other. The whole beauty of a task based approach (which is NOT putting the entire ai, physics, etc. in single threads) is that with a bunch of worker threads, you can easily do multithreading without creating a huge mess of dependencies and mutex locking.
If you find that your ai updates take too long, it might be a trivial matter of replacing the for-loop with a parallel_for.
Also pipelines can be good places for multithreading (already nicely supported by TBB, a library I'd consider almost perfect for a task based approach). A process like loading a level might require
-loading the data from disk
-uncompressing the data
-process the data (into vertex buffers or whatever)
-upload geometry/textures to the video card
Each step can be done in parallel and some steps can even be done in multiple threads. TBB usually takes care of assigning threads to these steps (you just define the steps and which can be done in parallel), but one way might be:
-1 thread loads files and unzips the data
-2 threads do the heavy lifting of processing the data
-1 thread (set to always be the main thread) uploads finished data to the video card.
Depending on existing code, this could be added in half a day, if load times turn out to be a problem. It's what I did with my Minecraft clone and the neat things is that it didn't screw with any other part of the code.
Though in general I'd agree that unless you see the need for it, don't burden yourself with the non-deterministic, sometimes almost impossible to track down subtle bugs that can't even be easily reproduced. If Dante would have known about computers, one level of hell would be debugging multithreaded applications for all eternity.
#10 Members - Reputation: 1058
Posted 25 August 2012 - 05:52 AM
I once wrote a distributed program which had multiple networked computers doing a bunch of processing. My first thought was to create LOTS of threads and assign them small tasks. Intuitively, this would seem to be very fast since the work is very small and broken down among multiple machines. Result: I was wrong.
The overhead cost of creating tons of small tasks and sending them through a network was more expensive than the gains made from multiprocessing.
There is a sweet spot for each job. More threads doesn't necessarily equal faster performance since there's always an overhead cost. If you're going to pay the overhead cost, make sure you're getting the most bang for your buck
Hobby: Game Developer
Currently employed as: Sr. Sharepoint Developer in Afghanistan
#11 Members - Reputation: 358
Posted 25 August 2012 - 09:05 AM
- 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.
Regards
Ömercan
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
#12 Moderators - Reputation: 14313
Posted 25 August 2012 - 09:56 AM
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.
Edited by Hodgman, 25 August 2012 - 10:03 AM.






